項目文件 > bin > public > routes > views app.js package.json
{
"name": "01-web-server-ssr", "version": "0.0.0", "private": true, "scripts": { "start": "nodemon ./bin/www" }, "dependencies": { "cookie-parser": "~1.4.4", "debug": "~2.6.9", "ejs": "~2.6.1", "express": "~4.16.1", "http-errors": "~1.6.3", "morgan": "~1.9.1" } }
var port = normalizePort(process.env.PORT || '3000'); app.set('port', port); var server = http.createServer(app);
它是經過http來作了一個服務器,默認端口是:3000node
這個文件中引入了一個app文件,這個文件是將createServer中的回調函數放到了外面,以模塊化的形式使用的,這個文件咱們叫它: '入口文件'web
- 1.應用級中間件 app.use(logger('dev')); // 日誌文件記錄 app.use(express.json()); // json數據格式化 app.use(express.urlencoded({ extended: false })); // 引入文件後綴名省略 app.use(cookieParser()); // cookie處理 app.use(express.static(path.join(__dirname, 'public'))); // 指定項目靜態資源文件夾爲public - 2.路由中間件 // http://localhost:3000/users app.use('/', indexRouter); app.use('/users', usersRouter); app.use('/',homeRouter) - 3.錯誤處理中間件 // catch 404 and forward to error handler app.use(function(req, res, next) { next(createError(404)); }); // error handler app.use(function(err, req, res, next) { // set locals, only providing error in development res.locals.message = err.message; res.locals.error = req.app.get('env') === 'development' ? err : {}; // render the error page res.status(err.status || 500); res.render('error'); });