nodejs redis 小试牛刀
一、环境安装
书接上文,浅学Redis之后,服务器已经安装Redis了,用 nodejs 链接 redis 之前,先安装 nodejs 环境。
1、安装fnm
(1)压缩包fnm-linux.zip搞到服务器上,我放在root里。
(2)解压、设置权限
unzip fnm-linux.zip
chmod 777 fnm
(3)设置环境变量,添加到/etc/profile文件末尾,配置生效
export PATH=$PATH:/root
source /etc/profile
(4)添加到~/.bashrc文件末尾
eval "$(fnm env --use-on-cd --shell bash)"
source ~/.bashrc
查看fnm版本命令验证是否安装成功
fnm --version
fnm 1.37.2
2、下载并安装 Node.js
fnm use --install-if-missing 20
3、验证 Node.js 版本
node -v
node: /lib64/libm.so.6: version `GLIBC_2.27' not found (required by node)
node: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by node)
node: /lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by node)
node: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by node)
node: /lib64/libc.so.6: version `GLIBC_2.28' not found (required by node)
node: /lib64/libc.so.6: version `GLIBC_2.25' not found (required by node)
4、安装依赖前,先换阿里云yum源(换过的请跳过)
(1)更换阿里云yum源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
(2)查看你安装的scl
yum list installed|grep "scl"
(3)删除scl重新安装
yum remove centos-release-scl.noarch
yum remove centos-release-scl-rh.noarch
yum install -y centos-release-scl centos-release-scl-rh
(4)配置scl国内源阿里云
文件 CentOS-SCLo-scl.repo
[centos-sclo-sclo]
name=CentOS-7 - SCLo sclo
baseurl=https://mirrors.aliyun.com/centos/7/sclo/x86_64/sclo/
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
文件 CentOS-SCLo-scl-rh.repo
[centos-sclo-rh]
name=CentOS-7 - SCLo rh
baseurl=https://mirrors.aliyun.com/centos/7/sclo/x86_64/rh/
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
(5)清理缓存
yum clean all && yum makecache
坑我踩过了,5、6、7、8 按这个顺序安装
5、升级 gcc(默认为4 升级为8)
yum install -y centos-release-scl
yum install -y devtoolset-8-gcc*
mv /usr/bin/gcc /usr/bin/gcc-4.8.5
ln -s /opt/rh/devtoolset-8/root/bin/gcc /usr/bin/gcc
mv /usr/bin/g++ /usr/bin/g++-4.8.5
ln -s /opt/rh/devtoolset-8/root/bin/g++ /usr/bin/g++
6、升级 make(默认为3 升级为4)
wget http://ftp.gnu.org/gnu/make/make-4.3.tar.gz
tar -xzvf make-4.3.tar.gz && cd make-4.3/
./configure --prefix=/usr/local/make --disable-dependency-tracking
make && make install
cd /usr/bin/ && mv make make.bak
ln -sv /usr/local/make/bin/make /usr/bin/make
7、升级 glibc
wget http://ftp.gnu.org/gnu/glibc/glibc-2.28.tar.gz
tar xf glibc-2.28.tar.gz
cd glibc-2.28/ && mkdir build && cd build
../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
make && make install
8、升级 libstdc++
wget http://www.vuln.cn/wp-content/uploads/2019/08/libstdc.so_.6.0.26.zip
unzip libstdc.so_.6.0.26.zip
cp libstdc++.so.6.0.26 /lib64/
cd /lib64
cp libstdc++.so.6 libstdc++.so.6.bak
rm -f libstdc++.so.6
ln -s libstdc++.so.6.0.26 libstdc++.so.6
“降龙8掌”打完了,收工
node -v
v20.17.0
二、服务端
1、初始化文件夹,装包:
- express Express.js简洁而灵活的 Node.js 框架
- cors 跨域中间件
- redis 客户端
- pm2 一个守护进程管理工具
2、修改配置
package.json -> scripts -> 添加:"start": "node ./index.js"
pm2安装后在项目目录下创建启动配置文件 ecosystem.config.js,代码如下:
module.exports = {
apps: [
{
name: 'first-api',
script: './index.js',
},
],
}
3、新建 index.js
const express = require('express');
const app = express();
const cors = require('cors');
const redis = require("redis");
const redisClient = redis.createClient({
url: 'redis://default:密码@IP:Port'
});
redisClient.on('error', err => console.log('Redis Client Error', err));
redisClient.connect();
// 使用cors中间件
app.use(cors());
app.get("/", (req, res) => {
res.send("Hello World");
})
app.get("/redis/keys", async (req, res) => {
var value = await redisClient.get("test")
res.send(value);
})
app.listen(3001, () => {
console.log("Server is running on port 3001");
})
4、pm2 start --watch,水灵灵启动
三、前端
1、一个请求的事
axios.get('http://localhost:3001/redis/keys').then(function (response) {
alert(response.data);
})
测试签名