1.controller/index.js 存放具体的执行代码
格式:
module.exports.fun_name = function(req,res){ } //对外暴露执行函数
2.router/route.js 管理所有的路由
格式:
var express = require("express");
var router = express.Router(); //引入express路由
var controller_1 = require("../controller/index.js"); //引入执行文件
router.route('req_path').get/post( controller_1.fun_name); //接受请求路径,分配执行文件中的具体方法
module.exports.router; //对外暴露路由
3.app.js 启动文件
格式:
var express = require("express");
var app = express();
var routes = require("./router/route.js"); //引入先前写好的路由文件
var HOST = '127.0.0.1';
app.use(express.static("public")); //处理静态资源
app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"); res.header("X-Powered-By",' 3.2.1') res.header("Content-Type", "application/json;charset=utf-8"); next();}); //处理头信息
app.use('/',routes); //把所有的请求交给route.js托管
app.listen(3000,HOST,function(){console.log("应用实例,访问地址为 http://"+ HOST +":3000")});