添加路由规则
听着李健的《传奇》,敲着dell的键盘,用着windows10的系统,我写下了这篇博客,关于express路由规则的添加。
app.js:
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'); var app = express(); // view engine setup app.set('port',process.env.PORT || 3000); app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); // uncomment after placing your favicon in /public //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); 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'))); routes(app); app.listen(app.get('port'), function(){ console.log('Express server listening on port ' + app.get('port')); });
index.js:
module.exports = function(app){ app.get('/',function(req,res){ res.render('index',{title: 'Express'}); }); app.get('/nswbmw',function (req,res){ res.send('hello.world!') }); };
记得改写命令后,ctrl+c,退出,重启,运行命令: node app.js
localhost:3000/nswbmw