zoukankan      html  css  js  c++  java
  • 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');
  • 相关阅读:
    acm课程练习2--1005
    acm课程练习2--1003
    [ZJOI2010]网络扩容
    [ZJOI2009]狼和羊的故事
    [FJOI2007]轮状病毒
    [NOIP2016提高组]换教室
    [NOIP2016提高组]愤怒的小鸟
    [NOIP2009提高组]最优贸易
    [洛谷P2245]星际导航
    [NOIP2013提高组]货车运输
  • 原文地址:https://www.cnblogs.com/ywjfx/p/10402448.html
Copyright © 2011-2022 走看看