preface

Select Node.js, Express, MongoDB to achieve a student information to add, delete, change and check.

  • The Express framework builds the server
  • Art-template template implementation page
  • Mongo database
  • Mongoose operates the database

The installation

  • npm install express mongoose
  • npm install art-template express-art-template
  • npm install body-parser bootstrap jquery

File directory

Mongo database

// student.js
const mongoose = require('mongoose')
const Schema = mongoose.Schema
mongoose.set('useFindAndModify'.false)
mongoose.connect('mongodb://localhost/mytest', {useNewUrlParser: true})
const studentSchema = new Schema({
  username: {
    type: String.require: true
  },
  gender: {
    type: Number.enum: [0.1].default: 0
  },
  age: {
    type: Number,},resume: {
    type: String}})module.exports = mongoose.model('Students', studentSchema)
Copy the code

routing

// router.js routing file
const express = require('express')
const router = express.Router()
const Student = require('./models/student')

// Home page display
router.get('/', (req, res, next) => {
  Student.find( (err, students) = > {
    if (err) {
      return res.status(500).send('Server error... ')
    }
    res.render('index.html', {
      students: students
    })
  })
})
// Page jump
router.get('/students/new', (req, res, next) => {
  res.render('topic/new.html')})/ / add
router.post('/students/new', (req, res, next) => {
  new Student(req.body).save( (err, ret) = > {
    if (err) {
      return res.status(500).send('Server error... ')
    }
    res.redirect('/')})})// Go to the edit page
router.get('/students/edit', (req, res, next) => {
  var id = req.query.id.replace(/"/g.' ')
  Student.findById(id, (err, student) => {
    if (err) {
      return res.status(500).send('Server error... ')
    }
    res.render('topic/edit.html', {
      student: student
    })
  })
})
/ / edit
router.post('/students/edit', (req, res, next) => {
  var id = req.body.id.replace(/"/g.' ')
  Student.findByIdAndUpdate(id, req.body, (err, ret) => {
    if (err) {
      return res.status(500).send('Server error... ')
    }
    res.redirect('/')})})/ / delete
router.get('/students/delete', (req, res, next) => {
  var id = req.query.id.replace(/"/g.' ')
  Student.findByIdAndDelete(id, (err, ret) => {
    if (err) {
      return res.status(500).send('Server error... ')
    }
    res.redirect('/')})})// Export interface
module.exports = router
Copy the code

Entrance to the file

// app.js entry file
const express = require('express')
const router = require('./router')
const bodyParser = require('body-parser')
const path = require('path')
const app = express()
const port = 5000
// Add Nodejs child_process module
// Use the default browser to open the address
const childProcess = require('child_process');

app.use('/node_modules', express.static(path.join(__dirname, 'node_modules')))
app.use('/public', express.static(path.join(__dirname, 'public')))
app.engine('html'.require('express-art-template'))
/ / configuration of the body - parser
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
app.use(router)
// Handle error requests uniformly
app.use( (req, res, next) = > {
  res.render('404.html')
})
app.listen(port, () => {
  console.log(`server runs on http://localhost:${port}`);
  // Use the default browser to open the address
  childProcess.exec(`start http://localhost:${port}`);
})
Copy the code

Implementation effect

supplement

You can download Nodemon to automatically monitor the changes of files and automatically start the server once the files change.

NPM install nodemon-g // Global installation

Nodemon app.js // Start the server using nodemon

conclusion

Once you’ve learned Node.js, test your learning with this relatively simple task. This project has been implemented for a week or two, I didn’t write it down as soon as possible, and now I have to go straight to the code.

Node.js is getting more and more interesting as you learn about it.