1.结构
node_modules ( ... )
routers ( main.js )
views ( index.html about.HTML 404.html )
app.js
package.json
2. 文件
package.json
{ "name": "wj", "version": "1.0.0", "description": "init somethings", "main": "index.js", "scripts": { "test": "six" }, "repository": { "type": "git", "url": "1.0" }, "keywords": [ "111" ], "author": "smilesix", "license": "ISC", "dependencies": { "cookie-parser": "^1.4.3", "ejs": "^2.6.1", "express": "^4.16.4", "http-proxy-middleware": "^0.19.1", "multer": "^1.4.1", "request": "^2.88.0" }, "devDependencies": {} }
app.js
var express = require("express"); var app = express(); // module var routers = require('./routers/main'); // ejs for view var ejs = require('ejs'); app.set('view engine', 'ejs'); app.engine('html', ejs.renderFile); routers.router(app); // all is Hello World! ( reg ) // app.get('/*', function(req, res){ // res.send("Hello World!"); // }); // not router // app.get('/', function(req, res){ // res.render('index.html'); // }); // app.get('/about', function(req, res){ // res.render('about.html'); // }); // 404 way1 // app.use(function(req, res) { // //res.send('my 404'); // res.render('404.html'); // }) var server = app.listen(5566, function(){ console.log("Server is running on http://localhost:5566"); });
main.js
var router = function (app) { app.get('/', function (req, res) { res.render('../views/index.html'); }); app.get('/about', function (req, res) { res.render('../views/about.html'); }); // 404 way2 app.get('*', function (req, res) { res.render('../views/404.html'); }); }; exports.router = router;
index.html about.HTML 404.html <h1>index/about/404 </h1>
3. 备注
测试网址: http://localhost:5566/sdfsdfsd
node 常用命令: 参考其他文章
express 常用命令 :参考其他文章