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行代码
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗