undefined不是函数:节点错误

时间:2015-07-26 20:48:17

标签: javascript node.js

每当我尝试运行static.js时,命令:

node static.js

我收到错误:

 libpath.exists(filename, function (exists) {
TypeError: undefined is not a function

这是我的static.js代码快照:

http.createServer(function (request, response) {

var uri = url.parse(request.url).pathname;
var filename = libpath.join(path, uri);

libpath.exists(filename, function (exists) {
    if (!exists) {
        response.writeHead(404, {
            "Content-Type": "text/plain"
        });
        response.write("404 Not Found\n");
        response.end();
        return;
    }

我在开头定义了libpath

var libpath = require('path')

1 个答案:

答案 0 :(得分:3)

存在函数未在路径模块中定义,它被定义为文件系统('fs')模块的一部分:

var fs = require('fs');

fs.exists(filename, function (exists) { });