Review node (4) to write a case in imitation of the community

path path operation module

path.basename: Get the path name of the file (including the extension).
path.dirname: Get the directory part of a path.
path.parse(): Convert the path into an object, which contains everything.
path.join: When you need to join paths, use this to join automatically.
Forget it, go to the path module on node's official website and try it yourself in the node environment.

Other members in Node

In each module, in addition to module-related APIs such as require and exports, there are two special members
__dirname that can be used to obtain the absolute path of the directory to which the current file module belongs (excluding the file name)
__filename can be used to obtain The absolute path of the current file (including the file name)
both dynamically obtain the path

Why do you want to say this problem, because in file operations, the use of relative paths is unreliable, because the path of file operations in Node is designed to be relative to the path where the node command is executed, so in order to solve this problem, it is necessary to use These two are solved because the path is obtained dynamically.
Insert picture description here
Insert picture description here
Insert picture description here
When designing the database here, pay attention to this date, not Date.now(), because it has already been called when new.
Insert picture description here
I need or here to judge whether it is repeated.
Insert picture description here
You can directly .json, and automatically transfer to json.
Insert picture description here
This forms a closed loop, front-end interaction, although it is just a simple registration page, but let me know more about how the front-end and back-end interact, and what they interact with , How to deal with interaction.

MD5 encryption

Insert picture description here

var md5 = require('blueimp-md5')
//对密码进行 md5 重复加密
body.password = md5(md5(body.password))

Insert picture description here

Server-side redirection is only valid for synchronous requests, and asynchronous requests are invalid.

In the Express framework, Session and Cookie are not supported by default, but we can solve it by using third-party middleware, express-session
Insert picture description here

var session = require('express-session')
//当我们配置好后,我们就可以通过req.session来访问和设置Session成员了
//添加Session数据:req.session.foo = 'bar'
//访问Session数据:req.session.foo
app.use(session({
    
    
    secret: 'keyboard cat',
    resave: false,
    saveUninitialized: true,
  }))

The session data will be lost as soon as the server restarts. Data can be persisted. He didn't talk about it here, only that it can be saved in the database through a third-party package. It shouldn't be too difficult. Here is to clear the login status.
Insert picture description here
The rest is almost the same, refer to the dark horse's node, go to koa clearly, look at the combination of vue and then write.

Guess you like

Origin blog.csdn.net/weixin_46013619/article/details/105244796