使用nodejs输入node example.js后 报错Unexpected identifier。由于急于先运行nodejs的实例,以初步了解,所以就不再纠结为什么了,直接找到了一个可以成功运行的方式。总结如下:
使用cmd,进入nodejs文件夹,比如我的nodejs安装在F盘下,
C:\Users\ylx>f:
F:\>cd program files
F:\Program Files>cd nodejs
然后运行同样保存在nodejs下的example.js文件
F:\Program Files\nodejs>node example.js
example.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}/`);
});
然后在浏览器中转到http://127.0.0.1:3000/,浏览器中显示出hello world。