Node ExpressJs server的路径设置

 

一、动态页面的路径:

app.METHOD(PATH, HANDLER)
Where:

* app is an instance of express.
* METHOD is an HTTP request method. get,post, put,delete,
* PATH is a path on the server.
* HANDLER is the function executed when the route is matched.

例:
1) app.get("/", f1);
2) app.post("/", f2);

把url路径的get/post操作直接映射到一个含response的函数,

f1: 可以是本模块中的文件, 或者其它模块中的文件, 也可以是一个Router模块

app.use("/", 单独的Router模块)

 

 

二、 静态文件的路由:

app.use(express.static('./public'));

** 还可以定义 虚拟的路径

app.use('/virtualPathName', express.static('./public'));

其中的目录,必须是基于网站的根目录, 与当前js文件的目录无关

var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));


例: 对images/kitten.jpg的访问:
app.use(express.static('public'));
http://localhost/images/kitten.jpg

如果用虚拟路径:
app.use('/static', express.static('public'));
http://localhost/static/images/kitten.jpg

http://expressjs.com/en/starter/static-files.html

http://expressjs.com/en/guide/routing.html

http://expressjs.com/en/4x/api.html#router

 

转载于:https://www.cnblogs.com/GameEngine/p/5218523.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值