nodejs redis 小試牛刀

冬先生發表於2024-10-25

一、環境安裝

書接上文,淺學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);
})

相關文章