centos7安装nodejs

方法一、https://github.com/nodesource/distributions#rpminstall

按照上面地址中的教程安装完后,使用node -v命令报错:

-bash: /usr/local/bin/node: 没有那个文件或目录 

使用: sudo node -v 则可以运行

使用:find / -name node 可以看到node命令这/usr/bin下面

方法二、https://www.scalescale.com/tips/nginx/install-nodejs-centos-7/

1.下载压缩包  wget http://nodejs.org/dist/v6.3.0/xxx.tar.gz

2.解压到安装目录 tar zxvf xxx.tar.gz

3. ./configure

4. make

5.make install

安装node错误:

wscript:329: error: Could not autodetect OpenSSL support. Make sure OpenSSL development packages are installed. Use configure --without-ssl to disable this message.

安装:yum -y install openssl-devel

创建一个node项目,新建example.js,输入以下内容

官网demo

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World
');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

运行: node example.js

本地访问该端口访问不到,原因是listen中的第二个参数已经写了只监听了本地环回地址了。把 listen 的第二个参数省略掉,就可以监听所有地址了。

PS: 另一个排错技巧:

用 netstat -lnp 也是可以看到一个程序到底监听的是 127.0.0.1 还是 0.0.0.0 的。

原文地址:https://www.cnblogs.com/oyx305/p/5655074.html