用Express-generator重新實做路由
目錄
先備知識
新增路由檔案
首先在routes底下新增我們自己的路由檔案news.js。
app/
└── routes
└── news.js
然後撰寫news.js的內容。
var express = require('express');
var router = express.Router();
router.get('/news', function(req, res, next) {
res.send('新聞:天氣晴');
});
module.exports = router;
修改app.js
增加新的路由檔案之後,要放入app.js使其在主程式執行時被啟用。
app/
└── app.js
app.js改成這樣:
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var newsRouter = require('./routes/news.js');//增加這行,引入路由
var app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/info', newsRouter);//增加這行,這樣我們要存取news的網址為http://localhost:3000/info/news
app.use(function(req, res, next) {
next(createError(404));
});
app.use(function(err, req, res, next) {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
這樣我們就能順利在
http://localhost:3000/info/news
存取到網站了。