node.js project and accumulation summary

①Port occupancy problem:

Running the server today, an error is reported when the node file is enabled, and the terminal prompts: Error: listen EADDRINUSE :::3000

After the query, it is found that two terminals are open, which causes the port to be occupied, and it can be closed.

②Database operation:

To connect to the database, you can use the mongoose module to operate mongodb for additions, deletions, and changes.

③require to load the module

require to load file code, you can load files with .js,.json,.node suffix, and the process is synchronous. so

setTimeout(() => {

  module.exports = { a: 'hello' };

}, 0)

require this file to get an empty object {}

④Apply

The application object usually represents an Express application. Create it by calling the top-level express() function exported by the Express module:

var express = require('express');

var app = express ();

app.get('/',function(req,res){

  res.send('hello world');

});

app.listen(3000);

⑤Callback function:

Node convention, if a function requires a callback function as a parameter, the callback function is the last parameter

The first parameter of the callback function itself is the error object passed in in the previous step.

⑥Core module:

http: Provides HTTP server functionality

url: Parse the URL

fs: interact with the file system

querystring: The query string to parse the URL

child_process: create a new child process

util: provides a series of useful gadgets

path: handle file path

crypto: Provides encryption and decryption functions, basically a wrapper around OpenSSL

⑦Exception handling:

Node is a single-threaded runtime environment, once the thrown exception is not caught, it will cause the entire process to crash. Therefore, the exception handling of Node is very important to ensure the stable operation of the system

Generally speaking, Node has three ways to propagate errors:

(1) Use the throw statement to throw an error object, that is, throw an exception

(2) Pass the error object to the callback function, and the callback function is responsible for issuing the error

(3) Issue an error event through the EventEmitter interface

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326691513&siteId=291194637