1

I'm new in node.js, I have learn node.js and express framework

I know how to run server, set router and render html template basicly, but all the tutorial tell render the html by res.send() or render how can I view static file?Like static sample.html:

just like http://127.0.0.1:8000/static.html

my file tree like:

root
|
|---server.js
|---static
    |---static.html

How can I view the static file by setting router?Or use some middleware(Better use express framework)?

Updated: I have try this:

    var express = require('express');
    var app = express.createServer();
    app.get('/', function(req, res){
        res.send('Hello World');
    });
    app.configure(function () {
        app.use(express.static(__dirname + '/static'));
    })

    app.listen(8000);

but it still can not work: tell me:Cannot GET /static/client.html

2 Answers 2

3

You can do this using the static middleware:

app.use(express.static(__dirname + '/static'));
Sign up to request clarification or add additional context in comments.

1 Comment

If the file name is static.html, then you should request 127.0.0.1:8000/static.html. If the file name is client.html, then you should request 127.0.0.1:8000/client.html.
1

Is your file named client.html as specified in your update or is it named static.html as you show in your file tree?

So, try either:

http://localhost:8000/client.html

or...

http://localhost:8000/static.html

Notice the lack of "/static" directory.

Also, put your app.configure line before your app.get.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.