NodeJS简单的网页跳转路由demo

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/laohoubin/article/details/46461143


demo目录结构



首先安装必要的环境,也就是NodeJS 库,使用npm install express jade 命令安装express 和jade

完成后,创建app.js

内容:

var express=require("express")
var port=process.env.PORT || 3000
var app=express()

app.set('views','./views')
app.set('view engine','jade')
app.listen(port);

console.log('imooc started on port '+port);

//index page
app.get('/',function(req,res){
	res.render('index',{
		title:'imooc 首页'
	})
})
//detail page
app.get('/movie/:id',function(req,res){
	res.render('index',{
		title:'imooc 详情'
	})
})
//admin page
app.get('/admin/movie',function(req,res){
	res.render('admin',{
		title:'imooc 后台录入页'
	})
})
//list page
app.get('/admin/list',function(req,res){
	res.render('list',{
		title:'imooc 列表页'
	})
})

创建list.jade\admin.jade\index.jade\detail.jade四个文件,里边的代码都为:

doctype
html
head
meta(charset="utf-8")
title #{title}
body
h1 #{title}


最后执行node app.js ,浏览器输入http://localhost:3000/即可看到首页效果,输入路由get的地址会跳转到相应的jade文件下。比如http://localhost:3000/admin/list会跳转到list.jade页面


猜你喜欢

转载自blog.csdn.net/laohoubin/article/details/46461143