Node.js开发环境及应用实例

node.jsnpmrundev

Node.js 简练,清爽

如何在IDEA中加入默认/内置函数的auto-completion
IDEA node.js 函数的autocomplete

实现了批量编辑文件名含有netsuite的所有文件,添加tags 和 categories

实现了月初的需求:寻求批量分类解决方案
这下面的代码可以灵活拓展,应用在不同的批量修改编辑磁盘目录下的文件内容。

Node.js源代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

var fs = require("fs");
var buf = new Buffer.alloc(1024);

console.log("查看 _posts 目录");
const strPostDir = "/Users/carlzeng/AppTesting/blog/source/_posts";
var intUpdatedCnt = 0;

fs.readdir(strPostDir, function (err, files) {
if (err) {
return console.error(err);
}

try {
files.forEach(function (file) {
// console.log( file );
// if (file == '0模版.md'){
if (file.toLowerCase().indexOf('netsuite') != -1) {
console.log(file);

//读取文件,并且替换文件中指定的字符串
let replaceFile = function (filePath, sourceRegx, targetStr) {
fs.readFile(filePath, function (err, data) {
if (err) {
return err;
}
let str = data.toString();

// console.info(str);

str = str.replace(sourceRegx, targetStr);

// console.info("AFTER: \n" + str);
fs.writeFile(filePath, str, function (err) {
if (err) return err;
});

});
}

replaceFile(strPostDir + '/' + file, /tags: \[\]/, "tags: [NetSuite] \ncategories: NetSuite")
intUpdatedCnt++;

if (intUpdatedCnt == 2000)
throw new Error("EndIterative");
}

});
} catch (e) {
if (e.message != "EndIterative") throw e;
}
});

Sublime Text 编辑器 打开文件夹目录

Sublime Text 编辑器 打开文件夹目录,它便会加载这个文件夹下的所有文件,(用这种方式更新文章,非常便捷)
然后使用菜单“查找 》 替换”来搜索特定的特征的字符串.
搜索内容,也是支持正则表达

Node.js Dev 步骤

  1. Git clone

    1. 用IDE编辑器(比如:WebStorm)git clone下来
  2. 打开IDE,Terminal Tab会自动切换到clone下来的当前nodejs目录

    1. npm install
      1. 如遇到版本问题(比如本机运行环境的版本太低)就去下载安装新版,或者直接把node可执行文件放到/usr/local/bin目录下(MAC环境,我放了四个文件版本就升级了:node,npm,npx,corepack)
    2. npm run dev
      1. 如遇到问题,可能需要安装特定的包,比如:npx patch-package
    3. [可选]npx vite build
  3. 可直接编辑Node JS代码

  4. 网络环境的情况下npm命令前面可能需要爬墙:

    1
    HTTPS_PROXY="http://127.0.0.1:7890" 
  5. 调试

    1
    2
    3
    4
    5
    6
    #On MacOS or Linux, run the app with this command:
    DEBUG=myapp:* npm start

    #On Windows Command Prompt, use this command:
    set DEBUG=myapp:* & npm start

Node.js Build 步骤

编译到当前项目的dist目录中: npm run build

详细步骤见

Node.js项目打包和发布的实现方法

Docker编译

​ docker build -t ipserver .

Node.js的Docker容器

目前完还不会打一整个docker的包,先搞一个Node.js的Docker容器,方便管理

1
2
3
4
5
6
7
8
9
10
11
12
13
version: "3"
services:
node:
stdin_open: true
tty: true
container_name: nodejs
restart: always
volumes:
- './app:/home/app'
ports:
- 3001:3000
image: node
command: bash

本小节思路来源:Mac上安装Node.js的Docker容器

Node.js的Docker Deployment步骤

  1. docker exec -it nodejs bash
  2. [可选步骤,仅适用于升级发布]
    1. ps -falx | head -1; ps -falx | grep ‘npm|node’
    2. kill -9 「node进程id,PPID值」
  3. node /home/app/blogsearch/bin/www &
    1. 在命令后面添加一个 &,让node运行在后台;回车以后,还有机会输入其他命令
    2. nohup node /home/app/blogsearch/bin/www &
      1. 回车以后,还有机会输入其他命令; 退出 docker exec -it 里面的bash后,程序还在后台运行

疑问:如何让这个node常驻在后台执行,意思是:当按键control+c退出当前docker exec -it nodejs bash时,不要退出这个进程。


即将上线

实测:Adding full text Search via FlexSearch to a Blog

起步:https://expressjs.com/en/starter/installing.html

1
2
3
4
5
6
7
8
9
10
11
12
13
mkdir blogsearch

cd blogsearch

npm init
entry point: (index.js) server.js

HTTPS_PROXY="http://127.0.0.1:7890" npm install express
added 62 packages, and audited 63 packages in 10s


HTTPS_PROXY="http://127.0.0.1:7890" npm install --save flexsearch
added 1 package, and audited 64 packages in 3s
1
2
3

const { Index, Document, Worker } = require("flexsearch");
const index = new Index();

思路来源:Adding full text Search via FlexSearch to a Blog