Now that I’ve had the time to learn more about Node, what do you want to make? After thinking about it, let’s make a simple bolg. Next, I will introduce the specific process.
Technology stack
- node
- Express (Web Application Framework)
- Mongoose (Database)
- Pm2 (Application Process Management)
- Swagger (API function debugging)
Project introduction
This article will introduce Node-app with specific items: github.com/Hancoson/no…
File structure
- App.js: entry file
- Package. json: Project information and package management
- Node_moudules: dependency module
- Public: static resources (CSS, JS, images)
- Routes: routes management
- View: template file
- Models: Data model
- Config: indicates the project configuration file
- App: store controllers, common methods, etc
- Bin: project configuration script
The MVC pattern
- Model
node
Provides modules, middleware, in useexpress
Generated when the project is creatednode_modules
It indicates that theM
layer- Modules such as
ejs
.mongoose
.morgan
.body-parser
, etc.
- View
express
Generated when the project is generatedviews
, that is, the front
- Controller
- That is, the view sends a request to the controller, and the controller selects the corresponding model to process it
- The model returns the results to the controller, which selects the appropriate view and generates an interface for the user
- Such as through
res.render
To renderejs
file
routing
- Meaning: Called when visiting the home page
ejs
Template engine renderingindex.ejs
file - Implementation method:
- in
app.js
writesrequire('./routes/index')(app)
Can be introduced; - The corresponding route is imported in the route
Controller
To achieve the display of specific dataapp.get('/articles/:id', articles.getArticle);
- in
Controller
It mainly deals with business logic, that is, how data should be displayed is managed by him. The specific implementation is as follows:
function(req, res) {blogdbs.find({_id: req.params.id // query condition},function (err, data) {
if (err) {
//err
} else {
res.render('articles', {... // Data object}); }})}Copy the code
Model
Let’s talk about Schema before we talk about Model, but what is Schema? It is similar to the table structure of a relational database. The concrete implementation is as follows:
var mongoose = require('mongoose');
var schema = mongoose.Schema;
var _blogSchema = new Schema({
title: {
type: 'String'},... });Copy the code
Model (modelName, schema);
Why have a Model when you have a Schema?
In Mongoose’s design concept, Schema only defines data structures, and the specific operations of adding, deleting, modifying and checking data are performed by Model.
It’s like Foxconn building a phone out of a mold. When you need to make a call, you use the phone instead of the mold.
Problems encountered
- Node development requires frequent project restarts, which I learned later
pm2
To manage the process and monitor itnode
Server update, no need to restart the service, greatly improve the development efficiency. - When the server commits data asynchronously,
app.js
The following code needs to be added to the route import.app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: false}));Copy the code
-
App.get (), app.use() and app.all()
- The callback in app.use(path,callback) can be either a Router object or a function
- Callback in app.get(path,callback) can only be a function. App.get () can be used as a shorthand for the get method in app.use().
- App.all () is the route attached to the application, so the app.Router middleware handles all routing requests, such as GET, POST, etc.
var express = require('express'); var app = express(); app.get('/hello'.function(req,res,next){ res.send('hello test2'); }); Var express = require('express'); var app = express(); var router = express.Router(); router.get('/'.function(req, res, next) { res.send('hello world! '); }); app.use('/hello',router);Copy the code
If you feel reading this article is helpful to you, please “Star”, your “Star” will be my biggest motivation to write!
Project address: Node-app