我在ubuntu上安装了nodejs并尝试运行我的第一个应用程序:
我跑了一个apache。
hello.js(保存在/ var / www / html / nodejs /中)是:
#!/usr/bin/env nodejs
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World! Node.js is working correctly!\n');
}).listen(8080);
console.log('Server running at http://localhost:8080/');
当我进入浏览器http://localhost:8080/时,我得到:
Hello World! Node.js is working correctly!
但是当我想在控制台中运行它时,我得到了这个:
sven@sven-MS-7464:~$ node /var/www/html/nodejs/hello.js
Server running at http://localhost:8080/
events.js:183
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::8080
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at Server.setupListenHandle [as _listen2] (net.js:1351:14)
at listenInCluster (net.js:1392:12)
at Server.listen (net.js:1476:7)
at Object.<anonymous> (/var/www/html/nodejs/hello.js:6:4)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
为什么我不能运行它?我需要做什么?
$ node -v
v8.9.4
$ nodejs -v
v8.9.4
$ npm -v
5.6.0
$ sudo npm config get proxy
null
$ sudo npm config
npm ERR! Usage:
npm ERR! npm config set <key> <value>
npm ERR! npm config get [<key>]
npm ERR! npm config delete <key>
npm ERR! npm config list [--json]
npm ERR! npm config edit
npm ERR! npm set <key> <value>
npm ERR! npm get [<key>]
npm ERR!
npm ERR! alias: c
答案 0 :(得分:1)
听起来你很困惑。
如果您可以在浏览器中访问它,那么您的节点应用程序正在正常运行。
当您尝试在控制台上运行它时,错误消息告诉您它已在运行!
你有什么期待?
答案 1 :(得分:1)
您没有关闭上一个节点进程,因此阻止端口8080(由Error: listen EADDRINUSE :::8080
消息指示)。
要释放它,请使用killall node
并再次运行脚本。
答案 2 :(得分:1)
根据提供的链接猜测您之前使用pm2
运行了该应用程序:
首先使用pm2 stop <your_app_name>
然后在控制台node <app_name>
上运行