router.js 656 B

123456789101112131415161718192021222324
  1. var express = require("express");
  2. var router = express.Router();
  3. //首页public/index.html
  4. router.get("/",function(req,res){
  5. res.sendFile(__dirname+"/"+"index.html");
  6. });
  7. //其他页面在public/views目录下
  8. router.get("/welcome",function(req,res){
  9. return res.render("admin/welcome");
  10. });
  11. router.get("/user",function(req,res){
  12. var data = {code:200,message:"success"};
  13. return res.send(JSON.stringify(data));
  14. });
  15. router.get("/user/:id",function(req,res){
  16. var data = [
  17. {id:1,name:'xxx',age:18},
  18. {id:2,name:'aaa',age:20},
  19. {id:3,name:"bbb",age:30}
  20. ];
  21. var id = req.params.id;
  22. return res.end(JSON.stringify(data[id]));
  23. });
  24. module.exports = router;