nodejs之express路由與動態路由


1、快速創建express項目步驟
/**
 *  1、cd 到項目里面
 *  2、npm init --yes  創建package.json文件
 *  3、安裝express
 *      npm install express --save-dev
 *  4、引入express使用
 *      var express = require('express');
 *      var app = new express();
 *      app.get('',function(req,res){
 *
 *      })
 *
 *     express:提供了路由、動態路由、中間件等
 */
2、利用express實現路由
var express = require('express');
var app = new express();

app.get('/',function (req,res) {
    res.send("hello express");
})


app.get('/news',function (req,res) {
    res.send("hello news");
})

//動態路由 http://localhost:8001/newscontent/1243
app.get('/newscontent/:aid',function (req,res) {

    //req.params獲取動態路由的傳值
    var aid = req.params.aid;  //aid = 1243
    res.send("hello newscontent"+"------"+aid);
})
//獲取get傳值  http://localhost:8001/product?aid=1234567
app.get('/product',function (req,res) {
    var aid =  req.query.aid;
    res.send("hello product----"+aid);
})
app.listen('8001','127.0.0.1');

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM