nodeJs文件运行中的语法错误

时间:2016-01-20 08:58:39

标签: javascript node.js

我对nodejs很新 当我试图运行我的第一个具有以下内容的js文件时

var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8081);
console.log('Server running at http://127.0.0.1:8081/');

使用命令

$ node fullpath / filename.js

我收到的错误如下所示 error message

任何人都可以说出错了什么。我正在使用nodejs应用窗口

1 个答案:

答案 0 :(得分:6)

请不要在REPL

中运行代码
[user@machine]$node
> node app.js
SyntaxError: Unexpected identifier
    at Object.exports.createScript (vm.js:24:10)
    at REPLServer.defaultEval (repl.js:225:25)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
    at REPLServer.<anonymous> (repl.js:417:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:210:10)
    at REPLServer.Interface._line (readline.js:549:8)
    at REPLServer.Interface._ttyWrite (readline.js:826:14)

只需在命令行中运行

即可
[user@machine]$node app.js
Server running at http://127.0.0.1:8081/