(添加) 所以我的下一个问题是,当我尝试添加一个新的依赖(npm install——save socket.io)。JSON文件也是有效的。我得到这个错误: 解析json失败

npm ERR! Unexpected string
npm ERR! File: /Users/John/package.json
npm ERR! Failed to parse package.json data.
npm ERR! package.json must be actual JSON, not just JavaScript.
npm ERR! 
npm ERR! This is not a bug in npm.
npm ERR! Tell the package author to fix their package.json file. JSON.parse 

我一直在试图弄清楚为什么这个错误一直返回。所有的文件(HTML,JSON,JS)都在我桌面上的同一个文件夹里。我使用node.js和socket.io

这是我的JS文件:

var app = require('express')();
var http = require('http').Server(app);

app.get('/', function(req, res){
  res.sendFile('index.html');
});

http.listen(3000,function(){
    console.log('listening on : 3000');
});

这是返回的内容:

MacBook-Pro:~ John$ node /Users/John/Desktop/Chatapp/index.js 
listening on : 3000
TypeError: path must be absolute or specify root to res.sendFile
    at ServerResponse.sendFile (/Users/John/node_modules/express/lib/response.js:389:11)
    at /Users/John/Desktop/Chatapp/index.js:5:7
    at Layer.handle [as handle_request] (/Users/John/node_modules/express/lib/router/layer.js:76:5)
    at next (/Users/John/node_modules/express/lib/router/route.js:100:13)
    at Route.dispatch (/Users/John/node_modules/express/lib/router/route.js:81:3)
    at Layer.handle [as handle_request] (/Users/John/node_modules/express/lib/router/layer.js:76:5)
    at /Users/John/node_modules/express/lib/router/index.js:234:24
    at Function.proto.process_params (/Users/John/node_modules/express/lib/router/index.js:312:12)
    at /Users/John/node_modules/express/lib/router/index.js:228:12
    at Function.match_layer (/Users/John/node_modules/express/lib/router/index.js:295:3)
TypeError: path must be absolute or specify root to res.sendFile
    at ServerResponse.sendFile (/Users/John/node_modules/express/lib/response.js:389:11)
    at /Users/John/Desktop/Chatapp/index.js:5:7
    at Layer.handle [as handle_request] (/Users/John/node_modules/express/lib/router/layer.js:76:5)
    at next (/Users/John/node_modules/express/lib/router/route.js:100:13)
    at Route.dispatch (/Users/John/node_modules/express/lib/router/route.js:81:3)
    at Layer.handle [as handle_request] (/Users/John/node_modules/express/lib/router/layer.js:76:5)
    at /Users/John/node_modules/express/lib/router/index.js:234:24
    at Function.proto.process_params (/Users/John/node_modules/express/lib/router/index.js:312:12)
    at /Users/John/node_modules/express/lib/router/index.js:228:12
    at Function.match_layer (/Users/John/node_modules/express/lib/router/index.js:295:3)

当前回答

如果您正在使用根目录,那么您可以使用这种方法

res.sendFile(__dirname + '/FOLDER_IN_ROOT_DIRECTORY/index.html');

但如果你使用的是在/Routes/someRoute.js文件夹中的Routes,那么你就需要这样做

const path = require("path");
...
route.get("/some_route", (req, res) => {
   res.sendFile(path.resolve('FOLDER_IN_ROOT_DIRECTORY/index.html')
});

其他回答

这个错误很简单。最可能的原因是index.html文件不在根目录中。

或者如果它在根目录中,那么相对引用将不起作用。

因此,您需要告诉服务器您的文件的确切位置。 这可以通过在NodeJs中使用dirname方法来实现。 只需将您的代码替换为以下代码:

 app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

确保在主页之前添加斜杠“/”符号。否则,您的路径将变成:rootDirectoryindex.html

而你希望它是:rootDirectory/index.html

尝试添加根路径。

app.get('/', function(req, res) {
    res.sendFile('index.html', { root: __dirname });
});

不幸的是,在ES6中,要访问绝对路径__dir_name,你必须在所有位置输入以下代码:

import { dirname } from 'path';
import { fileURLToPath } from 'url'

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

或者用你想要使用的函数创建一个path.js:

import { join, dirname } from 'path';
import { fileURLToPath } from 'url'

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

export { __dirname, join };

并在使用前导入: 导入{join, __dirname} from './path.js';

在app.js中的配置对我来说很好:

    //production mode
if (process.env.NODE_ENV === "production") {
  app.use(express.static(path.join(__dirname, "client/build")));
  app.get("*", (req, res) => {
    res.sendFile(path.join((__dirname + "/client/build/index.html")));

  });
}

//build mode
app.get("*", (req, res) => {
  res.sendFile(path.join(__dirname + "/client/public/index.html"));

});

这个错误很明显,你需要在res.sendFile()的配置对象中指定一个绝对路径(而不是相对路径)和/或设置根路径。例子:

// assuming index.html is in the same directory as this script

res.sendFile(__dirname + '/index.html');

或者指定一个根(它被用作res.sendFile()的第一个参数的基本路径:

res.sendFile('index.html', { root: __dirname });

当您传递用户生成的文件路径时,指定根路径更有用,该文件路径可能包含畸形/恶意部分,如..(如 . ../../../../../../ 等/ passwd)。设置根路径可以防止使用这种恶意路径访问该基本路径以外的文件。