我用
first.js
运行一段代码
var http = require("http");
http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Hello World"
response.end('Hello World\n');
}).listen(8124);
// Console will print the message
console.log('Server running at http://127.0.0.1:8124/');
当我用 gitbash 运行它时,它给出了所需的输出。但使用 Node js 命令提示符会给出 microsoft jscript 编译错误。
看起来您刚刚在终端窗口中输入了脚本的名称,因此要求窗口为您的脚本找到合适的解释器。您应该尝试使用 Nodejs 来解释脚本:
node your-script.js
不只是
your-script.js
这里的问题可能源于多个地方。最有可能基于安装。
以下代码(取自上面)
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n'); }).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
当我尝试将其加载到浏览器中时,会产生积极的结果。 因此我建议如下:
1.1 如果使用除纯文本编辑器之外的任何编辑器(notepad++、notepad、sublime 等),请确保文件实际上设置为输出纯文本(笨拙的移动但经常被忽视)
重新安装节点(值得一试)
如果所有其他方法都失败并且您已安装 npm,请输入“npm install http”并重新运行您的first.js 应用程序
对我有帮助的是 - 我在
--exec
文件的脚本部分中的 nodemon 命令之后删除了 package.json
。
"dev": "nodemon --exec src/index.js"
"dev": "nodemon src/index.js"
src
目录中。