美文网首页
Nodejs Error

Nodejs Error

作者: 前端xiyoki | 来源:发表于2017-04-01 14:34 被阅读0次

Cannot GET

  • 问题描述:
    npm start 后,进入localhost:3000,发现客户端浏览器显示 Cannot GET。
  • 产生的原因:
    express默认你的index.html位于项目根目录下的site目录下,且server.js位于项目根目录下。而当实际开发中这两个文件处于同一目录或index.html不处于site目录下时就会显示这个错误。
  • 解决办法:
  • 首先将server.js所处的路径保存到一个变量中:
// server.js
var application_root = __dirname;
  • 其次,如果两个文件处于相同目录下时:
//server.js
app.use(express.static(application_root));
  • 如果server.js与index.html分别位于不同的目录,且server.js位于根目录,index.html位于根目录下的dist目录时:
//server.js
var path = require("path");
app.use( express.static( path.join( application_root, 'dist') ) );

详见“CopyLeft”在问题Cannot GET下的回答。

相关文章

网友评论

      本文标题:Nodejs Error

      本文链接:https://www.haomeiwen.com/subject/iuyiottx.html