node基础

2021/11/10

# 静态资源服务器

const path = require("path");
const fs = require("fs");
const http = require("http");
const port = 8088;
const server = http.createServer((request, response) => {
    const reqPath = getRealPath(request.url);

    const reqUrl = path.join(__dirname, "./dist", reqPath);

    const responseFile = fs.readFileSync(reqUrl);
    response.end(responseFile);
});

server.listen(port, (error) => {
    if (error) {
        fs.appendFile('log.txt', error, 'utf-8', () => {});
    }
});

function getRealPath(reqUrl) {
    return reqUrl.startsWith('/assets') || reqUrl.endsWith('.html') ? reqUrl : path.join(reqUrl, "index.html");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 定时更新服务器

# deploy.sh
while true
do
    echo `date` >> log.txt
    git pull
    npm i
    npm run build

    mv dist dist_old
    mv  docs/.vuepress/dist dist

    rm -rf ./dist_old

    echo "完成更新:" `date +%H:%M:%S` >> log.txt

    sleepTime=`expr 60 \* 1`
    sleep ${sleepTime}
done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# express搭建

const express = require('express');
const app = express();
app.use('/', express.static('dist'));
const port = 8088;

app.listen(port, () => {
  console.log(`search-motion-docs listening at ${port}`);
});
1
2
3
4
5
6
7
8

# 设置淘宝镜像

npm config set registry https://registry.npm.taobao.org
1
  • 检查
npm get config registry
1

# 清空node打印台

process.stdout.write('\x1Bc')
1
上次更新: 11/1/2024