nodejs之express中间件路由使用
2024-08-31 08:12:42
1、express 中间件使用
/*
* 中间件:就是匹配路由之前和匹配路由之后做的一系列操作
*/ var express = require('express');
var app = new express(); /**
* 内置中间件:托管静态页面
*/
//http://localhost:8001/news
app.use(express.static('public')); //虚拟目录 http://localhost:8001/static/news
app.use('/static',express.static('public'));
//ejs中 设置全局数据 所有的页面都可以使用
app.locals['userinfo']='1213';
/**
* 中间件:表示匹配任何路由
* 应用级中间件
* next() 路由继续向下匹配
*/
app.use(function (req,res,next) {
console.log(new Date());
next();
}) app.get('/',function (req,res) {
res.send("hello express");
}) app.get('/news',function (req,res,next) {
//res.send("hello news");
console.log("news");
next();
}) app.get('/news',function (req,res) {
res.send("hello news1");
}) app.listen("");
最新文章
- AngularJS中的JSONP实践
- centos7查看端口命令
- Sprite Editor 图集切片精灵
- App开发者必备的运营、原型、UI设计工具整理
- maven auto-config 多环境自动打包
- jquery 中$.post获取MVC Controller中JsonResult返回包含LIst<;Model>;类型的子List<;Model>;的高级使用方法
- Dragger代码实现
- css6种隐藏元素的方法
- /etc/profile /etc/bashrc ~/.bash_profile ~/.bashrc ~/.bash_logout 说明及区别
- a标签传递参数
- 在模态框(Modal)中使用UEditor全屏显示的一个坑
- Robotics Tools
- 51nod 1636 教育改革
- The role of the inter-controller consensus in the placement of distributed SDN controllers
- SWD and JTAG selection mechanism
- tensor flow 安装
- [HNOI2008]玩具装箱
- 基于PCIe的高速接口设计
- Dalvik与JVM区别
- STM32之中断
热门文章
- zabbix分布式部署和主机自动发现
- Redis位操作介绍
- VSCode配合chrome浏览器调试cocos2d js项目
- Apache提示You don&#39;t have permission to access / on this server 解决
- Spring EntityResolver ";.dtd"; 和 ";.xsd";检验
- SELinux 对nginx访问目录的影响
- thinkPHP5.0.22初体验---request相关用法
- Python 文件I/OⅡ
- element-ui 弹出添加拖拽功能
- Trie树(字典树)整理