Node.js Development Environment and Application Examples

node.jsnpmrundev

Node.js: concise and refreshing

How to enable auto-completion for default/builtin functions in IDEA
IDEA node.js function autocomplete

Batch Editing of Files Containing “Netsuite” in Their Names, Adding Tags and Categories

Successful solution for the monthly requirement: seeking a batch categorization solution. The code below can be easily expanded and applied to modify content within disk directories.

The Node.js source code is as follows:

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
var fs = require("fs");
var buf = new Buffer.alloc(1024);

console.log("Viewing the _posts directory");
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);

// Read a file and replace specific strings within it
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 Editor: Opening a Directory

When opening a directory in the Sublime Text editor, it automatically loads all files within the folder (a convenient way to update articles). Then, utilize the “Find” menu and select “Replace” to search for specific strings. The search content also supports regular expressions.

Node.js Development Steps

  1. Git clone

    1. Clone using an IDE editor (e.g., WebStorm)
  2. Open the IDE, the Terminal Tab will automatically switch to the current nodejs directory from the clone

    1. npm install
      1. If encountering version issues (e.g., local runtime environment version too low), download and install a newer version, or directly place the node executable file in the /usr/local/bin directory (for Mac environment, I placed four file versions: node, npm, npx, corepack, and it upgraded)
    2. npm run dev
      1. If issues arise, specific packages might need installation, for example: npx patch-package
    3. [Optional] npx vite build
  3. Directly edit Node.js code

  4. In a network environment, it may be necessary to bypass firewalls before npm commands:

    1
    HTTPS_PROXY="http://127.0.0.1:7890" 
  5. Debugging

    1
    2
    3
    4
    5
    #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 Steps

Compile to the current project’s dist directory: npm run build

Detailed steps can be found in the article Node.js Project Packaging and Deployment Implementation

Docker Compilation

docker build -t ipserver .

Node.js Docker Container

Currently, I’m not familiar with building an entire Docker package, so I will first create a Node.js Docker container for better management.

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

This section is based on ideas from the article Installing Node.js Docker Container on Mac

Node.js Docker Deployment Steps

  1. docker exec -it nodejs bash
  2. [Optional steps, applicable only for upgrade deployment]
    1. ps -falx | head -1; ps -falx | grep ‘npm|node’
    2. kill -9 「node process id, PPID value」
  3. node /home/app/blogsearch/bin/www

Question: How can we ensure that this node persistently runs in the background, meaning that when exiting the current docker exec -it nodejs bash by pressing control+c, the process does not terminate?


Upcoming Release

Tested: Adding full text Search via FlexSearch to a Blog

To begin: https://expressjs.com/en/starter/installing.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
const { Index, Document, Worker } = require("flexsearch");
const index = new Index();

This idea is based on Adding full text Search via FlexSearch to a Blog