node.js - Express 在浏览器中返回 200,但使用 curl -I 返回 404

标签 node.js http express curl heroku

我有一个快速服务器,它提供一些静态文件。

当我从浏览器发出请求时,一切都很好。但是当我使用 cURL 时,它变得有点奇怪:

  • curl https://www.goukitok.com :带有页面内容的 HTTP 200
  • curl -I https://www.goukitok.com:HTTP 404(请参阅下面的详细日志)。

我需要 curl -I 才能工作,因为我的客户需要通过 Google AdWords 宣传此页面,而 Google 在允许他创建广告之前会执行检查。

我的服务器托管在 Heroku 上。

我不明白是什么造成的?你有什么想法吗?

提前致谢!


curl -v https://goukitok.com

* Rebuilt URL to: https://www.goukitok.com/
*   Trying 79.125.104.202...
* Connected to www.goukitok.com (79.125.104.202) port 443 (#0)
* found 148 certificates in /etc/ssl/certs/ca-certificates.crt
* found 597 certificates in /etc/ssl/certs
* ALPN, offering http/1.1
* SSL connection using TLS1.2 / ECDHE_RSA_AES_128_GCM_SHA256
*    server certificate verification OK
*    server certificate status verification SKIPPED
*    common name: www.goukitok.com (matched)
*    server certificate expiration date OK
*    server certificate activation date OK
*    certificate public key: RSA
*    certificate version: #3
*    subject: CN=www.goukitok.com
*    start date: Sat, 16 Dec 2017 23:42:00 GMT
*    expire date: Fri, 16 Mar 2018 23:42:00 GMT
*    issuer: C=US,O=Let's Encrypt,CN=Let's Encrypt Authority X3
*    compression: NULL
* ALPN, server accepted to use http/1.1
> GET / HTTP/1.1
> Host: www.goukitok.com
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: Cowboy
< Connection: keep-alive
< X-Powered-By: Express
< Access-Control-Allow-Origin: *
< Accept-Ranges: bytes
< Cache-Control: public, max-age=0
< Last-Modified: Fri, 02 Feb 2018 10:00:37 GMT
< Etag: W/"33cc-16155f5f188"
< Content-Type: text/html; charset=UTF-8
< Content-Length: 13260
< Set-Cookie: sessionId=s%3Akeguyz6fxoKCQDHIGY2Nd9mBgzM1pdiw.WhMMw37PNfXdv%2FYdx93oOdyTYMuv9SfcrYY0yuBrZ2E; Path=/; Expires=Tue, 13 Feb 2018 13:52:09 GMT; HttpOnly
< Link: <https://code.jquery.com/jquery-1.10.2.js>; rel=preload; as=script
< Link: <https://fonts.googleapis.com/css?family=Cuprum:400,700%7CNunito>; rel=preload; as=style
< Link: </static/libs/bootstrap/css/bootstrap.min.css>; rel=preload; as=style
< Link: </static/libs/animate/animate.css>; rel=preload; as=style
< Link: </static/libs/slick-slider/slick.css>; rel=preload; as=style
< Link: </static/libs/slick-slider/slick-theme.css>; rel=preload; as=style
< Link: </static/libs/fancybox/css/jquery.fancybox.css>; rel=preload; as=style
< Link: </static/libs/fancybox/css/jquery.fancybox-buttons.css>; rel=preload; as=style
< Link: </static/libs/fancybox/css/jquery.fancybox-thumbs.css>; rel=preload; as=style
< Link: </static/css/layout.css>; rel=preload; as=style
< Link: </static/css/components.css>; rel=preload; as=style
< Link: </static/css/responsive.css>; rel=preload; as=style
< Link: </static/libs/jquery/jquery-2.2.4.min.js>; rel=preload; as=script
< Link: <https://www.facebook.com/tr?id=1796165227077270&ev=PageView&noscript=1>; rel=preload; as=image
< Link: <https://maps.googleapis.com/maps/api/js?key=AIzaSyDqCiEbDSYSs-Wn-MHsq5jaygV4aew3_Tc&libraries=places>; rel=preload; as=script
< Link: </app/static/css/app.e595d7bef36fb3598585be947d8eb42a.css>; rel=preload; as=style
< Link: </app/static/js/manifest.1b6260866eb9b8932450.js>; rel=preload; as=script
< Link: </app/static/js/vendor.9159aedaca9691925a48.js>; rel=preload; as=script
< Link: </app/static/js/app.238040a9cfecf8b392ea.js>; rel=preload; as=script
< Link: </static/libs/bootstrap/js/bootstrap.min.js>; rel=preload; as=script
< Link: </static/libs/wow-js/wow.min.js>; rel=preload; as=script
< Link: </static/libs/slick-slider/slick.min.js>; rel=preload; as=script
< Link: </static/libs/isotope/isotope.pkgd.min.js>; rel=preload; as=script
< Link: </static/libs/fancybox/js/jquery.fancybox.min.js>; rel=preload; as=script
< Link: </static/libs/fancybox/js/jquery.fancybox-buttons.min.js>; rel=preload; as=script
< Link: </static/libs/fancybox/js/jquery.fancybox-thumbs.min.js>; rel=preload; as=script
< Link: </static/js/main.js>; rel=preload; as=script
< Link: </static/libs/parallax/jquery.parallax-scroll.min.js>; rel=preload; as=script
< Vary: Accept-Encoding
< Date: Tue, 06 Feb 2018 13:52:09 GMT
< Via: 1.1 vegur
< 
..... My page content
* Connection #0 to host www.goukitok.com left intact

curl -Iv https://goukitok.com

* Rebuilt URL to: https://www.goukitok.com/
*   Trying 79.125.111.38...
* Connected to www.goukitok.com (79.125.111.38) port 443 (#0)
* found 148 certificates in /etc/ssl/certs/ca-certificates.crt
* found 597 certificates in /etc/ssl/certs
* ALPN, offering http/1.1
* SSL connection using TLS1.2 / ECDHE_RSA_AES_128_GCM_SHA256
*    server certificate verification OK
*    server certificate status verification SKIPPED
*    common name: www.goukitok.com (matched)
*    server certificate expiration date OK
*    server certificate activation date OK
*    certificate public key: RSA
*    certificate version: #3
*    subject: CN=www.goukitok.com
*    start date: Sat, 16 Dec 2017 23:42:00 GMT
*    expire date: Fri, 16 Mar 2018 23:42:00 GMT
*    issuer: C=US,O=Let's Encrypt,CN=Let's Encrypt Authority X3
*    compression: NULL
* ALPN, server accepted to use http/1.1
> HEAD / HTTP/1.1
> Host: www.goukitok.com
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 404 Not Found
HTTP/1.1 404 Not Found
< Server: Cowboy
Server: Cowboy
< Connection: keep-alive
Connection: keep-alive
< X-Powered-By: Express
X-Powered-By: Express
< Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: *
< Content-Type: text/html; charset=utf-8
Content-Type: text/html; charset=utf-8
< Content-Length: 2644
Content-Length: 2644
< Etag: W/"a54-jADpSsEjSg0qJ+/8y3dImxntFHg"
Etag: W/"a54-jADpSsEjSg0qJ+/8y3dImxntFHg"
< Set-Cookie: sessionId=s%3ADWjJv6A4KYHEJcSdoxMABFcVXc6L-093.2lROCz0XKPu5dI1wmSOKgB3Ch%2FbPJQVaaS58zHXhLi8; Path=/; Expires=Tue, 13 Feb 2018 13:54:59 GMT; HttpOnly
Set-Cookie: sessionId=s%3ADWjJv6A4KYHEJcSdoxMABFcVXc6L-093.2lROCz0XKPu5dI1wmSOKgB3Ch%2FbPJQVaaS58zHXhLi8; Path=/; Expires=Tue, 13 Feb 2018 13:54:59 GMT; HttpOnly
< Vary: Accept-Encoding
Vary: Accept-Encoding
< Date: Tue, 06 Feb 2018 13:54:59 GMT
Date: Tue, 06 Feb 2018 13:54:59 GMT
< Via: 1.1 vegur
Via: 1.1 vegur

< 
* Connection #0 to host www.goukitok.com left intact 

最佳答案

我通过在路由文件的开头强制使用静态路径来解决这个问题。这是中间件顺序的问题

正如@PeterVC指出的here我的服务器未处理 HEAD 请求(使用 I 标志时由 cURL 发送)。

我刚刚将 app.use('/',express.static(path.join(__dirname, '../../dist'))) 移动到我的 Express 路由器的顶部文件。

关于node.js - Express 在浏览器中返回 200,但使用 curl -I 返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48644762/

相关文章:

javascript - 检查用户是否使用 PassportJS/ExpressJS 登录

node.js - 如何构建一个使用 React 构建的独立桌面应用程序,使用 Node 后端,并将前端和后端都 package 在 Electron 中作为桌面应用程序?

javascript - 从 ReactJS 应用程序调用 CMD 命令行

c++ - 局域网中存在的所有 IP 地址(即设备)的列表

javascript - 我的socket.io和node.js代码无法正常工作-为什么?

c# - 如何使nodejs代码在asp.net core(带有JavascriptService)应用程序中调用csharp代码?

iOS - 将 AFNetworking 与自定义 NSURLProtocol 类一起使用

java - 通过 Spring Rest 发出 MultiPart Put 请求,以使用 formData 调用 API(将 Apache MultipartEntityBuilder 替换为 REST)

c# - 如何使用 curl 将参数 POST 到 WCF 服务

javascript - 为什么我的 html 文件没有加载express.static?