express
1,安装 express
cd exp
npm install express
2,创建package.json
npm init
创建成文件如下所示
{ "name": "exp", "version": "0.0.1", "description": "test", "main": "index.js", "dependencies": { "express": "^4.15.0" }, "devDependencies": {}, "scripts": { "test": "good" }, "author": "", "license": "ISC" }
3,创建服务器
var express = require('express'); var app = express(); var router = express.Router([options]); // the sub app var admin = express(); admin.on('mount', function(parent) { console.log('Admin Mounted'); // console.log(parent); // refers to the parent app }); admin.get('/', function(req, res) { console.log(admin.mountpath); // /admin res.send('Admin Homepage'); }); app.use('/admin', admin); // mount the sub app var options = { dotfiles: 'ignore', etag: false, extensions: ['htm', 'html'], index: 'index.html', maxAge: '1d', redirect: true, setHeaders: function (res, path, stat) { res.set('x-timestamp', Date.now()) } } //静态资源文件夹绑定 app.use(express.static('public', options)) //根目录 app.get('/', function(req, res){ res.send('hello world'); }); //参数后缀 app.get('/users/:id.:format(json|xml)',function(req,res){ res.send(req.params.id+"<br>"+req.params.format); }) //参数正则匹配 app.get('/test/:id(\\d+)',function(req,res){ res.send(req.params.id); }) //路由 router.param('id', function(req, res, next, id) { console.log('CALLED ONLY ONCE'+req.params.id); next(); }); router.get('/user/:id', function(req, res, next) { console.log('although this matches'); next(); }); router.get('/user/:id', function(req, res) { console.log('and this mathces too'); res.send(req.params.id) res.end(); }); //使用路由 app.use(router); // 定制404页面 app.use(function(req, res){ res.type('text/plain'); res.status(404); res.send('404 - Not Found'); }); //定制500页面 app.use(function(err, req, res, next){ console.error(err.stack); res.type('text/plain'); res.status(500); res.send('500 - Server Error'); }); app.listen(3000);
4,debug
node debug index
1)cont 继续执行
2)next 跳到下一个语句
3)setBreakpoint(N) 设置断点
4)repl 启动node repl 允许查看变量值和执行代码
5)setp 进入当前函数中的语句
6)out 跳出当前执行函数
7)backtrace 显示当前调用执行的帧和调用栈
8)watch(expr) 向观察列表添加表达式
9)list(n) 列出调试器中当前停止行的前面和后面的n行代码