美文网首页
express.js代码

express.js代码

作者: sunny519111 | 来源:发表于2017-05-08 16:27 被阅读11次

简易目录

目录列表.png

1.简单的路由配置

// 输出模块
module.exports =  (app) => {
  app.get('/',(req,res) => {
    console.log(req.url)
    res.send('Hello world!')
  });
  //多个路由配置
  app.get('/customer',(req,res) => {
    console.log(req.url)
    res.send('customer pages')
  })

  app.get('/admin',(req,res) => {
    console.log(req.url)
    res.send('admin pages')
  })
}
// 引入模块
const express = require('express');
const app = express();
// 引入路由配置
const routes = require('./routes')(app)


app.listen(8080);

2. app的进化史

const express = require('express');
const http = require('http'); //http模块
const app = express();

// // 第一个中间件,next()方法可以执行下一个中间件app.use
// app.use((req,res,next) => {
//   //请求的路径名字
//   if(req.url == '/about'){
//     res.writeHead(200,{'Content-Type':'text/plain'});
//     res.end('Welcome to the about page!!\n');
//   }else {
//     next();
//   }
// });

// // 第二个中间件,通过第一个next()跳转到这个中间件
// app.use((req,res,next) => {
//   if(req.url == '/' ){
//     res.writeHead(200, { "Content-Type": "text/plain" });
//     res.end("Welcome to the homepage!\n");
//   } else {
//     next();
//   }
// });

// // 第三个中间件,通过第二个next()跳转到这个中间件
// app.use((req,res,next) => {
//   if(req.url == '/home' ){
//     res.writeHead(200, { "Content-Type": "text/html"},{"charset": "UTF-8"});
//     res.end("<head><meta charset='UTF-8'></head><h1>这是一个html的页面响应</h1>");
//   } else {
//     next();
//   }
// });

// // 第四个中间件,然后没有next() 结束了
// app.use(function(req, res) {
//   res.writeHead(404, { "Content-Type": "text/plain" });
//   res.end("404 error!\n");
// });

// http.createServer(app).listen(2000);



//简化上面的代码样式
// app.use("/home", (req, res, next) => {
//   res.writeHead(200, { "Content-Type": "text/plain" });
//   res.end("Welcome to the homepage!\n");
// });

// app.use("/about", (req, res, next) => {
//     res.writeHead(200, { "Content-Type": "text/html"},{"charset": "UTF-8"});
//     res.end("<head><meta charset='UTF-8'></head><h1>这是一个html的页面响应</h1>");
// });

// app.use("/jspage", (req, res, next) => {
//     res.writeHead(200, { "Content-Type": "application/javascript"},{"charset": "UTF-8"});
//     res.end("alert('hello')");
// });

// app.use((req, res) => {
//   res.writeHead(404, { "Content-Type": "text/plain" });
//   res.end("404 error!\n");
// });
// http.createServer(app).listen(1337);


//定义变量
app.set('views',__dirname+'/public');
app.set('view engine','jade');
// 上面代码使用set方法,为系统变量“views”和“view engine”指定值。

// 网站的重定向 设置在writeHead的前面
app.get('/redirect',(req,res) =>{
  res.redirect('http://www.baidu.com');
})

// 网页模板渲染
app.get('/render',(req,res)=>{
  res.render('index',{message:'hello world'});
});

// 使用express的use方法别名修改
 // 所有的中间件都会走这个流程
app.all("*", (request, response, next) => {
  response.writeHead(200, { "Content-Type": "text/plain", "charset":"utf-8"});
  next(); // 转到下一个中间件
});

app.get("/", (request, response) => {
  response.end("Welcome to the homepage!");
});

app.get("/about", (request, response) => {
  response.end("Welcome to the about page!");
});
// 模糊匹配参数
app.get('/params/:id',(req,res) => {
  res.end(`This is a text   ${req.params.id}`);
});

// 参数后面加上?表示可选的参数
app.get('/hello/:id?',(req,res)=>{
    if(req.params.id){
      res.end(`Hello, This is a ${req.params.id}`);
    }else{
      res.end('hello world')
    }
});


//上面的都匹配不到就会匹配这个
app.get("*", (request, response) => {
  response.end("404!");
});

http.createServer(app).listen(1337);

相关文章

网友评论

      本文标题:express.js代码

      本文链接:https://www.haomeiwen.com/subject/twzmtxtx.html