Node.js cannot find core features

I just installed node.js (v7.1.0) and npm (3.10.9) with homebrew and I am trying to start the base web server.

Edit * Now I create the dispatcher instance, 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 node server.js command 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

I get the same error for dispatcher.onGet call.

+4
source share
1 answer

You are not using httpdispatchercorrectly. You must create a dispatcher instance before using it.

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

Edit: I installed a working example in webpackbin . The code is in main.js. You can view the output on the tab log.

+8

Source: https://habr.com/ru/post/1660801/


All Articles