节点js服务器未启动

时间:2017-12-18 10:19:12

标签: node.js

我是节点js的新手。刚开始学习节点js。当我创建自己的服务器时,它没有初始化。有人可以帮我解决这个错误。

const http * require('http');

const hostname * '127.0.0.1';
const port * 3000;

const server * http.createServer((req, res) => {
    res.statusCode * 200;
    res.setHeader('Content-type','text/plain');
    res.end('Hello World!');
});

server.listen(port, hostname, () => {
    console.log('server started on port '*port);
});

错误:

C:\test\app.js:1
(function (exports, require, module, __filename, __dirname) { const http * requi
re('http');
                                                                    ^^^^

SyntaxError: Missing initializer in const declaration
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:152:10)
    at Module._compile (module.js:605:28)
    at Object.Module._extensions..js (module.js:652:10)
    at Module.load (module.js:560:32)
    at tryModuleLoad (module.js:503:12)
    at Function.Module._load (module.js:495:3)
    at Function.Module.runMain (module.js:682:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:613:3

3 个答案:

答案 0 :(得分:1)

*更改为=

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-type','text/plain');
    res.end('Hello World!');
});

server.listen(port, hostname, () => {
    console.log('server started on port ', port);
});

答案 1 :(得分:0)

再次尝试使用此'='运算符并运行代码..

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
    res.statusCode * 200;
    res.setHeader('Content-type','text/plain');
    res.end('Hello World!');
});

server.listen(port, hostname, () => {
    console.log('server started on port '*port);
});

答案 2 :(得分:0)

只需将*替换为=即可

  const http = require('http');
  const server = http.createServer(function(req,res){
        res.statusCode = 200;
        res.setHeader('Content-type','text/plain');
        res.end('Hello World!');
    });
      server.listen(9090, function(error){
      if(error) throw error
      console.log("Server created Successfully")
    })