zoukankan      html  css  js  c++  java
  • Express路由

    “路由”(routing):用于指定不同的访问路径所对应的回调函数,再简单点说就是返回结果和导航的作用。

    路由结构:

    app.METHOD(path, [callback...], callback)

    最直接的路由配置方法,就是调用app.get()、app.post();

    var express = require('express');
    var app = express();
    
    // respond with "hello world" when a GET request is made to the homepage
    app.get('/', function(req, res) {
      res.send('hello world');
    });
    
    module.exports = app;

    命令:npm start

    这里给网站添加了一条路由,get参数一是路径,参数二是回调函数,当用户访问网站根目录时,页面才会返回:Hello World

    访问http://localhost:3000/about 显示how about you输出

    var express = require('express');
    var app = express();
    
    // respond with "hello world" when a GET request is made to the homepage
    app.get('/', function(req, res) {
      res.send('hello world');
    });
    
    app.get('/about', function(req, res) {
      res.send('how about you?');
    });
    
    module.exports = app;

     结合模块的使用:

    var express = require('express');
    var app = express();
    
    //指定模板引擎
    app.set("view engine", 'jade');
    
    //指定模板位置
    app.set('views', __dirname + '/views');
    
    //利用模板文件home.jade渲染为html
    app.get("/", function(req, res) {
        res.render('index.jade', {
            title: 'tinyphp'
        });
    });
    
    module.exports = app;

    更多实例请参考:http://www.expressjs.com.cn/guide/routing.html

  • 相关阅读:
    mark::开源绘图工具graphviz
    bzoj1013球形空间产生器sphere 高斯消元(有系统差的写法
    背包专题练习
    仿射加密与S-DES加密算法的实现
    1178:成绩排序
    1177:奇数单增序列
    1176:谁考了第k名
    1311:【例2.5】求逆序对
    1310:【例2.2】车厢重组
    1175:除以13
  • 原文地址:https://www.cnblogs.com/tinyphp/p/4939163.html
Copyright © 2011-2022 走看看