Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
node.js什么导致错误400错误请求_Node.js_Express_Http_Caching_Http Error - Fatal编程技术网

node.js什么导致错误400错误请求

node.js什么导致错误400错误请求,node.js,express,http,caching,http-error,Node.js,Express,Http,Caching,Http Error,我正在向node.js web应用程序添加一些代码。我添加了这个特性,然后它抛出了一个错误400。我通过按Ctrl-Z删除了它,但它仍然抛出错误400。然后,我做了一个test.js,它是express最简单的实现,但仍然出现错误400。下面是我的test.js代码: const app = require("express")(); const http = require("http").createServer(app); const url = require('url'); app.

我正在向node.js web应用程序添加一些代码。我添加了这个特性,然后它抛出了一个错误400。我通过按Ctrl-Z删除了它,但它仍然抛出错误400。然后,我做了一个test.js,它是express最简单的实现,但仍然出现错误400。下面是我的test.js代码:

const app = require("express")();
const http = require("http").createServer(app);
const url = require('url');

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

http.listen(3001, function()
{
    console.log("--listening on port 3001");
});
我已检查以确保键入了正确的url和正确的端口。我认为有些东西被缓存了,并且把它搞砸了,因为如果我清除缓存或者使用curl,它就会工作。有什么想法吗?

使用process.cwd()而不是导致错误并生成404的_dirname。 process.cwd()将返回初始化节点的目录。它返回启动node.js进程的绝对路径

const app = require("express")();
const http = require("http").createServer(app);
const url = require('url');
const path = require('path');

app.get("/", function(req, res)
{
    //  res.sendFile(__dirname + "/test.html");
    res.sendFile(process.cwd() + "/test.html");
});

http.listen(3001, function()
{
    console.log("--listening on port 3001");
});
或者也可以解析_dirname的路径

const app = require("express")();
const http = require("http").createServer(app);
const url = require('url');
const path = require('path');

app.get("/", function(req, res)
{
    __dirname=path.resolve();  
    res.sendFile(__dirname + "/test.html");

});

http.listen(3001, function()
{
    console.log("--listening on port 3001");
});
使用process.cwd()而不是导致错误并生成404的_dirname。 process.cwd()将返回初始化节点的目录。它返回启动node.js进程的绝对路径

const app = require("express")();
const http = require("http").createServer(app);
const url = require('url');
const path = require('path');

app.get("/", function(req, res)
{
    //  res.sendFile(__dirname + "/test.html");
    res.sendFile(process.cwd() + "/test.html");
});

http.listen(3001, function()
{
    console.log("--listening on port 3001");
});
或者也可以解析_dirname的路径

const app = require("express")();
const http = require("http").createServer(app);
const url = require('url');
const path = require('path');

app.get("/", function(req, res)
{
    __dirname=path.resolve();  
    res.sendFile(__dirname + "/test.html");

});

http.listen(3001, function()
{
    console.log("--listening on port 3001");
});
经过更多的研究(并感谢评论),我终于发现了问题
我在cookies中存储了太多东西,超过了4KB的最大容量

经过更多的研究(感谢评论),我终于找到了问题

我在cookies中存储了太多东西,超过了4KB的最大容量

要确认/消除缓存,您是否在其他浏览器上尝试过?尝试从命令行发出请求(例如
curlhttp://localhost:3001/
)我刚刚尝试了curl命令。它按预期工作。确实是缓存导致了问题。要确认/消除缓存,您是否在其他浏览器上尝试过?尝试从命令行发出请求(例如
curlhttp://localhost:3001/
)我刚刚尝试了curl命令。它按预期工作。这肯定是缓存造成的问题。