[英]Node js beginner getting an error
this is my code for a simple node.js application: 这是我的一个简单node.js应用程序的代码:
var http = require('http');
var fs = require('fs');
var path = require('path');
var url = require('url');
var port = process.env.port || 1337;
http.createServer(function onRequest(req, res) {
var urlParts = url.parse(req.url);
var doc = '/docs' + urlParts.pathname;
path.exists(doc, function fileExists(exists) {
if (exists) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
fs.createReadStream(doc).pipe(res);
} else {
res.writeHead(404);
res.end('Not Fouind\n');
}
});
}).listen(port);
When I try to run it I get an error that reads: 当我尝试运行它时,我收到一条错误,内容如下:
path.exists(doc, function fileExists(exists) {
^
TypeError:Undefined is not a function
This is copied off of a tutorial so I'm not sure what's going on. 这是从教程中复制的,所以我不确定发生了什么。 (PS: I'm using Visual Studio) (PS:我正在使用Visual Studio)
I think path.exists
is deprecated. 我认为path.exists
已被弃用。 I've been using fs.exists
. 我一直在使用fs.exists
。 Try that. 试试吧。
fs.exists(doc, function fileExists(exists) {
if (exists) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
fs.createReadStream(doc).pipe(res);
} else {
res.writeHead(404);
res.end('Not Fouind\n');
}
});
path.exists
is not a function, the function exists in the fs
module under fs.exists
. path.exists
不是函数,函数存在于fs.exists
下的fs
模块中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.