node. JS Project Directory Introduction

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.