javascript - Node.js 找不到基本功能

标签 javascript node.js

我刚刚用自制软件安装了 node.js(v7.1.0) 和 npm(3.10.9),我正在尝试运行一个基本的 Web 服务器。

编辑* 我现在实例化调度程序但仍然得到相同的错误


var http = require('http');
var port = 8080;
var HttpDispatcher = require('httpdispatcher');
var http           = require('http');
var dispatcher     = new HttpDispatcher();

dispatcher.setStaticDirname(__dirname);
dispatcher.setStatic('');

dispatcher.onGet("/page1", function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Page One');
});

var server = http.createServer().listen(port);


server.on('request', function (req, res) {
    console.log('GOT');
    dispatcher.dispatch(req, res);  
});

当我运行命令 node server.js 时出现此错误

dispatcher.setStaticDirname(__dirname);

TypeError: dispatcher.setStaticDirname is not a function
    at Object.<anonymous> (/Users/NodeJS/node_modules/server.js:5:12)
    at Module._compile (module.js:573:32)
    at Object.Module._extensions..js (module.js:582:10)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)
    at Module.runMain (module.js:607:10)
    at run (bootstrap_node.js:420:7)
    at startup (bootstrap_node.js:139:9)
    at bootstrap_node.js:535:3

我在 dispatcher.onGet 调用中遇到了同样的错误。

最佳答案

您没有正确使用 httpdispatcher。您必须在使用前实例化调度程序。

var HttpDispatcher = require('httpdispatcher');
var dispatcher     = new HttpDispatcher();

关于javascript - Node.js 找不到基本功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40612325/

相关文章:

javascript - 使用react和webpack : use the non-minified dev environment

javascript - 身份验证后无法从 Firebase 读取数据

arrays - 如何读取从nodejs中的req.session加载的数组的长度?

javascript - 如何将 jQuery 函数的结果传递给 PHP

javascript - 我需要帮助弄清楚为什么这个 'if' 语句不起作用

javascript - 如何处理nodejs中同一get函数的多次调用?

javascript - 如何将 Express.js 变量传递给 MongoDB 函数?

node.js - nodejs + mongodb错误异常: FieldPath 'progress' doesn't start with $

javascript - Google App 引擎 Nodejs Puppeteer 中的全局变量

javascript - 悬停时运行不同的模式