Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.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 是否有一个“问题”;res.status()”是什么;?_Node.js - Fatal编程技术网

Node.js 是否有一个“问题”;res.status()”是什么;?

Node.js 是否有一个“问题”;res.status()”是什么;?,node.js,Node.js,我刚刚开始构建一个web服务器和一个API。我想发送响应,但出现错误 我看过多部关于一个故事的纪录片 res.staus(200).json({Some Json}) 但我只是犯了个错误 这是我的密码: const http = require("http"); const server = http.createServer((req, res, next) => { res.status(200).json ({ messag

我刚刚开始构建一个web服务器和一个API。我想发送响应,但出现错误

我看过多部关于一个故事的纪录片

res.staus(200).json({Some Json})
但我只是犯了个错误

这是我的密码:

    const http = require("http");

    const server = http.createServer((req, res, next) => {
        res.status(200).json ({
            message: "should get this"
        });
    });

    server.listen(3000);
当我输入
localhost:3000
时,我希望在web浏览器中获得json,但我没有

我得到一个错误:

res.status(200).json({
^
TypeError:res.status不是一个函数

我在官方的Node.js http文档中找不到该函数,所以我的问题是:有res.status函数吗?如果没有,为什么会有这么多的示例使用它?

状态(200)。json(输出)是表示语法。http语法如下:

res.statusCode = 404;
res.write('your json string');
// Or if you want to end the output writing
res.end('your json string');
还可以使用
writeHead
方法指定状态代码,例如:

res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('okay');

祝你好运,在这里找到关于http nodejs模块的更多信息:

我认为
createServer
应该接收express应用程序,而不是中间件。