Node.js找不到基本功能 [英] Node.js cannot find basic functions

查看:83
本文介绍了Node.js找不到基本功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I just installed node.js(v7.1.0) and npm(3.10.9)with homebrew and I am trying to run a basic web server.

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

Edit* I now instantiate the dispatcher but still get the same error

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);  
});






当我运行命令节点server.js时我得到此错误


when I run the command node server.js I get this error

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调用。

I get the same error for the dispatcher.onGet call.

推荐答案

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

You are not using httpdispatcher correctly. You must instantiate the dispatcher before using it.

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

这篇关于Node.js找不到基本功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆