app.use和app.get与express.static有什么区别? [英] What's the difference between app.use and app.get with express.static?

查看:117
本文介绍了app.use和app.get与express.static有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:原来我的问题不是中间件express.static(),而是app.use()和app.get()之间的区别.这个问题可以完美回答(比表达API文档更好!):

NOTE: Turns out my issue isn't the middlware express.static(), but rather the difference between app.use() and app.get(). This question answers it perfectly (way better than the express API docs!):

app.use和app.get之间的区别在express.js中

我理解app.use('/')和app.get('/')之间的区别在于,后者仅向该端点提供HTTP GET请求,而前者向该端点提供所有HTTP请求.

I understand the difference between app.use('/') and app.get('/') to be that the latter only serves HTTP GET requests to that endpoint, while the former serves ALL HTTP requests to that endpoint.

我还了解到express.static中间件可提供从目录路径到端点的静态页面.

I also understand that express.static middleware serves static pages from a directory path to an endpoint.

我不明白的原因是这样的:

What I don't follow is why this:

app.get('/', express.static(__dirname + '/public')

仅服务于所请求的第一页,而不服务所请求页面所引用的任何ref =或src =链接/脚本页面.例如,这是两个响应简单的index.html页面的morgan跟踪,该页面具有指向文件'style.css'的css链接

Serves only the first page requested, and not any of the ref= or src= link/script pages referenced by the requested page. For example, here are two morgan traces responding to a simple index.html page that has a css link to file 'style.css'

1)使用app.use('/')

1) Server request trace using app.use('/')

Server listening on 0.0.0.0:8080
GET / 200 6.258 ms - 444
GET /style.css 304 2.842 ms - -

2)使用app.get('/')

2) Server request trace using app.get('/')

Server listening on 0.0.0.0:8080
GET / 304 5.131 ms - -
GET /style.css 404 2.990 ms - 22

404 ???

即使浏览器向/发送了GET请求,怎么办app.get('/')却无法为CSS提供服务,但是app.use('/')成功了.

How is it that even though the browser sent a GET request to '/', app.get('/') failed to serve the css, but app.use('/') succeeded.

app.get('/')或express.static我缺少什么细节?

What detail am I missing with app.get('/') or express.static?

预先感谢, PT

这是简单的代码:

app.js:

var morgan = require('morgan'),
    express = require('express'),
    app = express(),
    server = require('http').Server(app);
app.use(morgan('dev'));

   // Uncomment .get or .use, but not both

   // this only serves index.html, not style.css when I nav to localhost:8080/
   //app.get('/', express.static(__dirname + '/pub'));

   // this serves both pages when I nav to localhost:8080/
   app.use('/', express.static(__dirname + '/pub'));

server.listen(8080);

这是html ...

And here's the html...

index.html

index.html

<!doctype html>
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="style.css">
  </head>
</html>

路径:

/app.js
/pub/index.html
/pub/style.css

推荐答案

app.get('/', handler)是添加/到路由表,以及何时HTTP GET请求到达呼叫处理程序"

app.get('/', handler) is "add / to routing table, and when http GET request arrives call handler"

app.use(middlevare)是将中间值添加到堆栈中".

app.use(middlevare) is "add middlevare to the stack".

中间件"是一个接受(request, response, next)的函数,下一个中间件由上一个用next()显式调用

"middleware" is a function which accepts (request, response, next), next middleware is explicitly called by previous one with next()

express.static()返回中间件-具体来说,该函数检查请求的路径并流式传输相应文件的内容以进行响应.如果使用app.get('/') 添加它,则永远不会调用非"/"路线

express.static() returns middleware - specifically, a function that checks path of request and streams content of a corresponding file to response. If you add it using app.get('/') it's never called for non-"/" routes

这篇关于app.use和app.get与express.static有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆