node.js 11 Web framework introduced Express, installation, static pages, routing

Refer to the original article -http: //bjbsair.com/2020-03-22/tech-info/2815/
Introduced in front of the file created node.js modules, http server and static websites. With this knowledge as a basis, we can find out the framework node.js the Web.

Come all the way from Java Web Framework friends may feel quite heavyweight, like the original Struts and later the Spring, Apache intermediate organizations also had some other template framework, in general, either from use or learning is concerned, it is it takes more time. For large-scale applications, the investment is worth it. If you need to develop a small web application in a short time, it is questionable.

Fortunately, with the emergence of node.js, there has been like a lightweight Express, a flexible web framework, development of small and medium speed web applications have a rapid improvement.

node.js 10 Web framework introduced Express, installation, static pages, routing

express.js

node.js Web framework

Java world, Spring is used currently in the absolute superiority. But other languages ​​not the same, regardless of node.js or python, there are several web application framework. In particular node.js, web frameworks more, the industry and even named its top 10 node.js web framework. But do not panic, remain the same, a lot of node.js web frameworks are developed on the basis of the Express. This is why I will appear node.js. in the series of node.js Recent hot framework koa Express also before a bunch of developers do later, later I will write a special introduction Koa.

The reason why the introduction Express, first, because many are node.js framework on the basis of the Express, another reason is very lightweight Express, get started quickly, coming to do a CRUD time can be shortened to a few hours or less. It reminds me of some years ago, I use Java Blueprint EJB do a CRUD took about two days, including server configuration, debugging on a different web server. However node.js not need these, because http server and Application server are programmers themselves with node.js code is written. At another level, the real large-scale enterprise applications, I personally do not recommend node.js. Small, lightweight application, node.js is preferred.

Express JS

On Github harvest 47.7k stars, now is the most downloaded of node.js framework. node.js blog I introduced the system before using the web framework Ghost is the Express.

Static website can provide services through the Express, routing, middleware, and used in conjunction with the template engine.

Express JS installation and operation

Installation by npm.

  1. 安装express “npm install express --save”
D:\Projects\nodejs\NodeDemo\forms>npm install express --save  
npm WARN [email protected] No description  
npm WARN [email protected] No repository field.  
  
+ [email protected]  
added 50 packages from 37 contributors and audited 127 packages in 11.149s  
found 0 vulnerabilities
  1. After completing the first step, express the installation has been done locally. But no way to create a framework like create-react-app did. Fortunately, there is an express program generator, express-Generator . The command "npm install express-generator -g" to install.
D:\Projects\nodejs\NodeDemo\forms>npm install -g express-generator  
D:\Program Files\nodejs\node_global\express -> D:\Program Files\nodejs\node_global\node_modules\express-generator\bin\express-cli.js  
+ [email protected]  
added 10 packages from 13 contributors in 6.346s
  1. Run "express" command to create a project file

Since we have completed the forms project directory, run directly express so you can create project files and subdirectories in the directory. If the project did not create directory number, you can run the "express". This command will automatically create a directory, and create the associated subdirectories and files in the directory.

D:\Projects\nodejs\NodeDemo\forms>express  
  
  warning: the default view engine will not be jade in future releases  
  warning: use `--view=jade' or `--help' for additional options  
  
destination is not empty, continue? [y/N] y  
  
   create : public\  
   create : public\javascripts\  
   create : public\images\  
   create : public\stylesheets\  
   create : public\stylesheets\style.css  
   create : routes\  
   create : routes\index.js  
   create : routes\\users.js  
   create : views\  
   create : views\error.jade  
   create : views\index.jade  
   create : views\layout.jade  
   create : app.js  
   create : package.json  
   create : bin\  
   create : bin\www  
  
   install dependencies:  
     > npm install  
  
   run the app:  
     > SET DEBUG=node10:* & npm start

Note that we are running, the system prompted "destination is not empty, continue? [Y / N]", directly enter "y" enough. This is because we are created in the forms directory and the directory has been part of the project file, "npm -i" created.

  1. Run "npm install" express install dependent packages
D:\Projects\nodejs\NodeDemo\forms>npm install  
added 4 packages from 3 contributors, removed 2 packages, updated 16 packages and audited 141 packages in 12.091s  
found 0 vulnerabilities
  1. Run Project "npm start"
D:\Projects\nodejs\NodeDemo\forms>npm start  
  
> [email protected] start D:\Projects\nodejs\NodeDemo\forms  
> node ./bin/www  
  
GET / 200 17.703 ms - 207  
GET /stylesheets/style.css 200 5.473 ms - 111

At this point, open your browser and go to http: // localhos: 3000 / to display the home page express creation of the project.

node.js 10 Web framework introduced Express, installation, static pages, routing

expressjs homepage

ExpressJS static page processing

A benefit of using a framework that does not require a lot of processing the underlying knock yourself through the code line by line, the value of the framework is that it can provide a lot of packaged interface.

In dealing with static pages, we do not need to read through the fs module file back to the client by the response. In express, you only need to call response.render () method, passing in the template file name.

Sample Code: /routes/index.js

//引入express  
var express = require('express');  
var router = express.Router();  
  
/* 获取静态页面 */  
router.get('/', function(req, res, next) {  
  res.render('index', { title: 'Express' });  
});  
//导出抹开  
module.exports = router;

The code above extracts from the code express projects created, you can only see the return of static pages need to introduce express module, call routing Router, then get method, read the index template file called response.render (), the static template files can be returned to the client. Note here that there is a {title: 'Express'} is a template parameter, we will be introduced later.

Just a few lines of code, no people around logic, static page processing is complete.

ExpressJS route

In the above process static pages, we have used the route Router. Specific use of the following routes:

  1. The actual introduction of the routing processing in the inlet app.js file . For example, we deal with the actual routing static page index is /routes/index.js, then we can introduce index app.js route router through the following code
var indexRouter = require('./routes/index');
  1. Set the static page / template directory
app.set('views', path.join(__dirname, 'views'));  
......  
//中间省略数行代码  
......  
app.use(express.static(path.join(__dirname, 'public')));

__dirname here represents the current directory, this line of code specifies a static page / template directory.

3. Specify the relative path by the routing processing app.use

app.use('/', indexRouter);

Said here, when accessing the site, the system uses the index routing process, namely /routes/index.js.

And we talked about earlier in index.js too, the system calls the res.render () method passing the template file that static pages back to the browser.

ExpressJS dynamic page processing

The method by res.send ().

router.get('/', function(req, res, next) {  
  res.send('respond with a resource');  
});

The sample code can be viewed /routes/users.js

This is the processing path routing / users, the routing information can be written in the app.js.

var usersRouter = require('./routes/users');  
app.use('/users', usersRouter);

When the user through a browser to http: // localhost: When 3000 / users, the system will call /routes/users.js in response.get () method returns the information to the client.

node.js 10 Web framework introduced Express, installation, static pages, routing

Routing users

Original articles published 0 · won praise 0 · Views 301

Guess you like

Origin blog.csdn.net/zxjoke/article/details/105082167
Recommended