Node.js notes using express to create a static file HTTP server

In the last note, I wrote an HTTP service that provides access to static files using node.js. In this note, I intend to use the express framework under node.js to build the same HTTP service.

The instructions for installing express, and using the express generator to generate an express project, will not be repeated here. There is a detailed introduction on the official website of express, you can refer to: http://www.expressjs.com.cn/starter/installing.html

Only record the code written here as a record of learning and subsequent use.

The directory structure I created is simple:


app.js : is the main application;

index.html : the default home page provided by the static http server;

public: folder, used to store static files, I store css here, and files such as img can also be stored here.

 

Take a look at the implementation of app.js:

 

var express = require('express');
var app = express ();

app.use(express.static('public'));

app.get('/', function (req, res) {
    // res.send('Hello World!');
    res.sendfile('index.html');
});

var server = app.listen(3000, function () {
    var host = server.address().address;
    var port = server.address().port;

    console.log('Example app listening at http://%s:%s', host, port);
});

 

 

Key code description:

1、app.use(express.static('public'));

According to the introduction on the express official website, this statement is to use the public folder in the project directory as a folder for static resources. express.static() is the static file middleware provided by express. Regarding the middleware of express, I understand that it is similar to the pipeline of node.js. When a corresponding request starts, it can be processed by different middleware.

2、

app.get('/', function (req, res) {

    // res.send('Hello World!');

    res.sendfile('index.html');

});

The routing function of express is used here. For the get method of http, when the request path is "/", the index.html in the root directory is directly returned.

Start app.js on the command line and enter the address in the browser: http://localhost:3000 to see the effect:


 Here, there is no need to enter the file name to be accessed, and the file jump is realized directly through the defined route.

Finally, I did another experiment and created chunxiao.html in the public folder. The above routing implementation code has been modified, and the page to which the routing jumps is cuanxiao.html under the public folder.

 

app.get('/', function (req, res) {
    // res.send('Hello World!');
    res.sendfile(__dirname + '/public/chunxiao.html');
});

 Change the content of chunxiao.html slightly and remove the last sentence. Rerun the program, enter the address in the browser, and check the effect:

 


 You can see that the actual output here is our modified chunxiao.html

 

Finally, I used express's generator to generate a new app: myapp, which is ready for the follow-up practice.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326614374&siteId=291194637