简体   繁体   中英

Can't run this nodejs code?

Im trying to run this simple code but I m getting error all the time...I really dont know what's the problem. It seems to me very Ok...? Thanks!

    var http = require(http);
    console.log('Starting');
    var host = '127.0.0.1';
    var port = 1337;

    var server = http.createServer(function(request, response){

            console.log('Receive request: '+ request.url);
            response.writeHead(200, {"Content-type" : "text/plain"});
            response.end("Hello world");


    });
 server.listen(port, host, function(){
        console.log('Listening: ' + host + ':' + port);
    });

Console error is this:

assert.js:98
  throw new assert.AssertionError({
        ^
AssertionError: path must be a string: path must be a string
    at Module.require (module.js:362:3)
    at require (module.js:380:17)
    at Object.<anonymous> (/Users/yoniPacheko/PhpstormProjects/angularJS/nodeJS/server.js:9:12)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

在您的第一行中,http 周围缺少引号:

var http = require('http');

In my case, I had

var http = require('http');
var path= require('path');

As soon as I deleted the the http line, it worked for me.So, i did leave only

var path= require('path');

I was working on express,mongodb,socket.io.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM