The new project structure should be like this
Bin: The project's startup file, or other scripts can be placed.
Node_modules: The dependent library used to store the project.
Public: Used to store static files (css,js,img).
Routes: Routing controller.
Views: View directory (equivalent to V in MVC).
App.js: Project entry and program startup file.
Package.json: Package description file and developer information.
----------------------------------------------- Other, create your own using ------------------------------
Models: Data model (equivalent to M in MVC).
Controllers: Controller, operation on request (equivalent to C in MVC).
Tools: Tool Library.
Config: Configure directory.
Test: Tests the directory.
README.MD: Project description file.
App.js A simple description of the file and set the App.js file as the startup file.
Module dependencies
var express = require (' Express ');
var path = require (' path ');
var favicon = require (' Serve-favicon ');
var logger = require (' Morgan ');
var cookieparser = require (' Cookie-parser ');
var bodyparser = require (' Body-parser ');
var routes = require ('./routes/index ');
var users = require ('./routes/users ');
Add, Import HTTP Module
var http=require (' http ');
var app = Express ();
Set the port number
App.set (' Port ', Process.env.PORT | | 3000);
View engine settings
App.set (' Views ', Path.join (__dirname, ' views '));
App.set (' View engine ', ' Ejs ');
Load Environment variables
App.use (Logger (' dev '));
App.use (Bodyparser.json ());
App.use (bodyparser.urlencoded ({extended:false}));
App.use (Cookieparser ());
App.use (Express.static (Path.join (__dirname, ' public '));
Load route
App.use ('/', routes);
App.use ('/users ', users);
Boot and port
Http.createserver (APP). Listen (App.get (' Port '),
function () {
Console.log (' Express server listening on port ' + app.get (' port '));
});
Load Error Handling workaround
App.use (function (req, res, next) {
var err = new Error (' not Found ');
Err.status = 404;
Next (ERR);
});
if (App.get (' env ') = = = ' Development ') {
App.use (function (err, req, res, next) {
Res.status (Err.status | | 500);
Res.render (' Error ', {
Message:err.message,
Error:err
});
});
}
App.use (function (err, req, res, next) {
Res.status (Err.status | | 500);
Res.render (' Error ', {
Message:err.message,
Error: {}
});
});
Exporting an App Object
Module.exports = app;
node. JS Project Directory Introduction