Simple configuration and database connection of NodeJS build project
The main contents are as follows:
- Load exprss module
- Create app app
- Load template processing
- Set up static file hosting
- Load database module
- Blog offline
- Processing post requests
- Cookie settings
Load exprss module
var express = require('express');
Create app application (equivalent to NodeJS Http.createServer();)
The code block syntax follows the standard markdown code, for example:
var app = express();
Load template processing module (used to process templates such as HTML)
var swig = require('swig');
- Configure the application template and define the template engine used by the current template
app.engine('html',swig.renderFile); //Parameters: 1. The name of the template engine, which is also the suffix of the template file; 2. The method used to parse and process the template content
- Define template storage directory
app.set('views','./views'); //Parameter: the first parameter must be views, and the second is directory
- Template engine for registration
app.set('view engine', 'html'); //Parameter: the first parameter must be view engine, and the second parameter corresponds to the first parameter of engine
- Cancel template cache (in development mode)
swig.setDefaults({ cache: false });
Set static file hosting (such as css,image and other static files)
- For example, when the url accessed by the user starts with public, the file under the corresponding "dirname +" / public "will be returned directly
app.use('/public',express.static(__dirname + '/public'));
Load database module
var mongoose = require('mongoose');
//Connect to database
mongoose.connect('mongodb://username:password@host:port/database?options...',{useMongoClient: true},function(err){
if(err){
console.log(err);
}else{
console.log('Database connection successful');
//Listen for http requests
app.listen(8081);
}
});
**Note: username and password are the user name and password corresponding to the selected database. host is the ip address and port is the port number. options are optional.
Load the body parser and process the data submitted by post
var bodyParser = require('body-parser');
app.use( bodyParser.urlencoded({extended: true}) );
Cookie related settings
- First, load the cookies module
var Cookies = require('cookies');
req.cookies = new Cookies(req, res);
- Store cookie, set method (at login)
//Take setting cookie: userInfo for example
req.cookies.set('userInfo',JSON.stringify({
_id: userInfo._id,
username: userInfo.username
}));
- Get cookie, get method
req.cookies.get('userInfo')
- An example of the full version is as follows:
1.Obtain cookie
app.use( function(req, res, next){
req.cookies = new Cookies(req, res);
//Resolve cookie information of login user
req.userInfo = {};
if(req.cookies.get('userInfo')){
try{
req.userInfo = JSON.parse(req.cookies.get('userInfo'));
}catch (e){}
}
next();
});
2.storage cookie
responseData.userInfo = {
_id: userInfo._id,
username: userInfo.username
};
req.cookies.set('userInfo',JSON.stringify({
_id: userInfo._id,
username: userInfo.username
}));
2018/4/8