【问题标题】:Getting error while running nodejs code about syntaxerror运行有关语法错误的nodejs代码时出错
【发布时间】:2019-09-25 20:20:16
【问题描述】:

当我在终端中运行 node.js 代码时出现错误。我是 node.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\n');
});

server.listen(port, hostname, () => {
 console.log(`Server running at http://${hostname}:${port}/`);
});

当我在终端运行代码时出现错误:-

(function (exports, require, module, __filename, __dirname) { {\rtf1\ansi\ansicpg1252\cocoartf1561\cocoasubrtf600
                                                           ^

SyntaxError: Invalid or unexpected token
at new Script (vm.js:80:7)
at createScript (vm.js:274:10)
at Object.runInThisContext (vm.js:326:10)
at Module._compile (internal/modules/cjs/loader.js:664:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)

【问题讨论】:

  • 它对我来说很好用。我正在使用节点 8.10.0
  • 您发布的代码不会为我重现任何错误。
  • 你用什么程序来编辑你的 Nodejs 程序?它不是文字处理器,对吧?
  • 我正在使用节点 10.15.3
  • 我在 mac 中使用 textedit 进行编辑

标签: javascript node.js


【解决方案1】:

需要将当前文件文本转换为纯文本格式, 去 Format-> Make Plain Text 然后点击 OK 按钮。然后用 .js 扩展名保存文件。

【讨论】:

    猜你喜欢
    • 2016-04-15
    • 2019-11-24
    • 2018-01-24
    • 2017-05-16
    • 1970-01-01
    • 2017-05-18
    • 2021-06-15
    • 2021-08-18
    • 1970-01-01
    相关资源
    最近更新 更多