The first section:

Initialize the project

mkdir my-project

cd my-project

NPM init // entry point:(index.js) change to app.js

Edit package.json and add a script command “start”: “node app”

2. Install

npm i express --save-dev

3. Create app.js(entry file) in the root directory, edit the following code, and run NPM start



Go to http://localhost:3000/


The second section:

1. Using a template engine, using PUG (one of the template engines) in the project

npm i pug --save-dev


Add two lines of code to the app.js file to set up the template engine

app.set(‘views’, path.join(__dirname, ‘views’));

app.set(‘view engine’, ‘pug’);

3. Create a view folder and create an index.pug edit



Section 3: Connecting to mongodb

1. Installation:



npm i mongoose --save-dev

2. Create the mongo. Js

const mongoose = require(‘mongoose’)

const DB_URL = ‘mongodb://localhost:27017/test’

mongoose.connect(DB_URL)

Mongoose.connection. on(‘connected’, () => console.log(‘ started mongo’))

3. Start mongodb (go to the bin directory of mongodb).

Mongod –dbpath c:\ mongodb-data \db // mongod –dbpath

4. Run CMD to go to the bin directory of mongodb.

Mongo / / to enter

Show DBS // Check the database

Show collections // View tables in the database

The fourth section:

1. The post request uses body-Parser, which is obtained by req.body

npm i body-parser

app.use(bodyParser.urlencoded({ extended: false })); application/jsonapp.use(bodyParser.json()); 

2. Set the static file

app.use(express.static(path.join(__dirname, "public")));

3. Introduce third-party packages through Bower

npm i -g bower

To create a.bowerrc file, write the following code

{ "directory": "public/bower_components", "registry": "https://registry.bower.io"}

bower install bootstrap

Section 5 (MonodB add, Delete, change and check) :

/ / delete

 Article.findById(req.params.id, function(err, article){ 

  Article.remove(req.params.id, function(err){ 

   if(err){

     console.log(err);

   } else {

       res.send('Success');

       res.redirect(‘/’)

     }

  }); 

});

/ / modify

const article = {}

article.title = req.body.title

article.author= req.body.author

article.content= req.body.content



Article.update({_id: req.params.id),article,(err)=>{

  if(err) {

    cosnole.log(err)

  } else{

       res.send({res_code: 1})

   }

}

increase

app.post(‘/articles/add’, function (req, res) {  

  const { title, author, content } = req.body 

  const article = new Article() 

  article.title = title 

  article.author = author 

  article.content = content 

  article.save((err)=>{ 

    if(err){ 

      console.log(err) 

       return 

    } else{ 

        res.redirect(‘/’)

    } 

  })

});



Section 5 Front-end Code Specification EditorConfig and ESLint:

npm i eslint -g

eslint --init 

To initialize the ESLint configuration, select:

-> Use a popular style guide

-> Standard

-> JSON

Create a new.editorConfig file in the root directory

Create a new.eslintignore file under myblog

Json: {“lint”: “eslint — ext.js app.js node-study-demo SRC –fix”}

Eslint will not get an error message until vscode downloads eslint