Node.js —— 使用express模块创建静态web服务器及其路由
2017-11-24 14:33
1131 查看
1、使用express创建静态web服务器
首先使用命令行在当前路径创建目录public(可自定义),并在目录下创建文件test.txt(可自定义)
在 node.js 文件中指定静态目录(./public是静态目录)
通过命令行访问静态目录下的静态文件(“/test.txt”是静态目录下的文件的相对路径)
2、路由的创建方法
路由:将不同的请求,分配给相应的处理函数
(1)path 方法,即直接填入路由的相对路径
命令行下执行
首先使用命令行在当前路径创建目录public(可自定义),并在目录下创建文件test.txt(可自定义)
$ mkdir public $ vim test.txt //(文件内容自定义)
在 node.js 文件中指定静态目录(./public是静态目录)
app.use(express.static("./public"));
通过命令行访问静态目录下的静态文件(“/test.txt”是静态目录下的文件的相对路径)
$ curl http://localhost:18001/test.txt
2、路由的创建方法
路由:将不同的请求,分配给相应的处理函数
(1)path 方法,即直接填入路由的相对路径
app.get('/add',function(req,res){ res.end('path /add\n'); })
命令行下执行
$ curl http://localhost:18001/add[/code]
(2)路由的Router方法,用于创建在一个路由下的多个子路由var Router=express.Router(); Router.get('/add',function(req,res){ res.end('Router /add\n'); });// 创建子路由add Router.get('/list',function(req,res){ res.end('Router /list\n'); });// 创建子路由list app.use('/post',Router);// post是主路由
命令行下执行$ curl http://localhost:18001/post/add) $ curl http://localhost:18001/post/list)[/code]
(3)路由的route方式,用于创建一个路由在不同方法下的不同处理app.route('/article') .get(function(req,res){ res.end("route /article get\n"); })// 对get方法的处理 .post(function(req,res){ res.end("route /article post\n"); });// 对post方法的处理
命令行下执行(-X用于指定方法,若不指定,默认为GET)$ curl -X POST http://localhost:18001/article $ curl -X GET http://localhost:18001/article
3、路由参数的使用
创建路由参数
语法:app.param( name,function ),name是路由参数的名字,function是路由参数的处理函数app.param('newsId',function(req,res,next,newsId){ req.newsId=newsId; next(); });
使用路由参数app.get('/news/:newsId',function(req,res){ res.end("newsId:"+req.newsId+"\n"); });
命令行下输入$ curl http://localhost:18001/news/123
完整代码:var express=require("express");
var app=express();
// 使用express创建静态服务器
app.use(express.static("./public"));
// 路由的path方法,即直接填入文件路径
app.get('/add',function(req,res){ res.end('path /add\n'); })
// 路由的Router方法
var Router=express.Router();
Router.get('/add',function(req,res){
res.end('Router /add\n');
});
Router.get('/list',function(req,res){
res.end('Router /list\n');
});
app.use('/post',Router);
// 路由的route方式
app.route('/article')
.get(function(req,res){
res.end("route /article get\n");
})
.post(function(req,res){
res.end("route /article post\n");
});
// 路由参数
app.param('newsId',function(req,res,next,newsId){ req.newsId=newsId; next(); });
app.get('/news/:newsId',function(req,res){
res.end("newsId:"+req.newsId+"\n");
});
app.listen(18001,function afterListen(){
console.log("express running on http://localhost:18001"); });
4、使用express模板
1、安装express-generatornpm install -g express-generator
2、在当前文件夹中,创建 express 项目express [项目名称]
3、进入 express 项目,并安装相关依赖包cd [express项目的路径] npm install
4、执行express项目(express的执行脚本是bin/www)node bin/www
5、关于中间件
1、connect:node.js的中间件框架
2、分层处理
3、每层实现一个功能
4、预处理中间件
5、后置处理中间件// 后置处理中间件 ResellerSchema.post('save',function (next) { console.log('this is ResellerSchema post save middleware'); next(); }); // 预处理中间件 ResellerSchema.pre('save',function (next) { console.log('this is ResellerSchema pre save middleware'); next(); });
相关文章推荐
- Node.js —— 使用http模块创建静态web服务器及其路由
- 第1章-安装Node.js及相关要点-1.7.使用Node.js的HTTP模块来创建一个简单服务器
- node.js框架 服务器express的使用 及 route路由的使用
- 建立node.js服务器(使用express搭建Web环境)
- node.js学习(六、express创建web服务器)
- Node.JS 使用 Express 创建 Node.js Web 项目
- 建立一个node.js服务器(使用express搭建第一个Web环境)
- 在Node.js环境下使用Express创建Web项目
- Node.js —— 使用net模块创建TCP服务器/客户端
- 为node.js服务器添加静态网页,express模块。
- Node.js使用Express创建Web项目详细教程
- Node.js Web 模块-创建web服务器
- 【node.js】使用node.js搭建一个本地web服务器
- Nodejs+Express创建HTTPS服务器
- 使用 Node.js 搭建 Web 服务器
- 使用express创建node.js工程
- node.js中创建服务器与路由构建
- 使用nodejs的http模块创建web服务器
- 使用Node.js的socket.io模块开发实时web程序