javascript - Node.js cannot find basic functions - Stack Overflow

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

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

when I run the mand 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._pile (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

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

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

when I run the mand 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._pile (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

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

Share Improve this question edited Nov 15, 2016 at 14:49 Brandon Bryant asked Nov 15, 2016 at 14:27 Brandon BryantBrandon Bryant 1691 silver badge14 bronze badges 2
  • 1 Did you add httpdispatcher on your package.json and run npm install ? – Enrichman Commented Nov 15, 2016 at 14:30
  • I ran the mand 'npm install httpdispatcher' and still am getting the same error – Brandon Bryant Commented Nov 15, 2016 at 14:38
Add a ment  | 

1 Answer 1

Reset to default 10

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744834310a4596171.html

相关推荐

  • javascript - Node.js cannot find basic functions - Stack Overflow

    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

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信