//引入http系统模块
var http = require("http");
//创建一台服务器
var server = http.createServer();
//注册用户请求事件
server.on("request",function(req,res){
//req. 请求,res 响应
res.write("hello world");//反馈信息
res.end(); //结束响应
});
//监听接口
server.listen(3000);
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 index = require('./routes/index');
//载入前台文章详情的路由文件
var article = require('./routes/posts');
var users = require('./routes/users');
//----------------------二级路由------------------
//创建一个应用,里面包含了创建服务器
var app = express();
// view engine setup 设置项目的目录是Views目录
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');//项目使用的模块引擎是ejs.
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));//使用express中间件,可以把中间件理解成一个函数
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
//----------------------一级路由------------------
//前台首页面
app.use('/', index);//核心,表示访问Localhost:3000
//前台详情页面
app.use('/posts', article);
app.use('/users', users);
//----------------------一级路由------------------
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// 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');
});
module.exports = app;
index.js
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
//render(index) 响应views视图的index页面
module.exports = router;
posts.js
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('article', { title: 'Express' });
});
//render(article) 响应views视图的article页面
module.exports = router;
下面 自己编写前台index,article页面