The Express framework is a simple and flexible Node.js Web application framework. Previously, we manually created the server in Express as an API, which allowed us to focus more on the functionality and development efficiency of the business, and not too much on the bottom line.

Express Chinese official website: Express

Quick start

  1. NPM install Express –save

  2. Introduced into the project:


const express = require('express')

// Get the server instance

const app = express()

// The binding server accepts the request event and adds a processing callback function

app.get('/', (req, res) => res.send('Hello World! '))

// Bind service ports to start the server

app.listen(3000, () = >console.log('Example app listening on port 3000! '))
Copy the code
  1. Operating Projects:node app.js

Using the Express framework allows us to reduce the amount of code we need and is much more intuitive than building server code typesetting using the Node core module HTTP. The API of the HTTP core module is used at the bottom of Express. If we want to handle different client request paths, we can list multiple app.get() methods down the list without using if… else… To judge. Express also does not require us to set the content-Type and Chinese encoding format of the response header.

const express = require('express')
const app = express()
app.get('/', (req, res) => {
  res.send('Hello China')
})
app.get('/login', (req, res) => {
  res.send('< h1 > login < / h1 >')
})
app.get('/register', (req, res) => {
  res.send('< h1 > register < / h1 >')
})
app.listen('3000', () = > {console.log('running... ')})Copy the code

Static services in Express

preface

In the process of web site back-end development, we often need to expose some static folders, users can access the content according to the URL address, these static files are often called public resources, express framework can be easily hosted static files.

API address of this section: Express hosting static files

  1. Create a new public folder (public Resources) in your project:public

  1. Expose common resources using Express’s static file service
  • Method 1 :(common)

app.use('/public/', express.static('./public'))

The first parameter specifies that the user must use a URL starting with /public/ to access the specific file resource in the static folder. Express.static () passes a relative path to the file to expose.

let express = require('express')
let app = express()
app.use('/public/', express.static('./public'))
app.listen(3000, () = > {console.log('running... ')})Copy the code

  • Method 2:

app.use(express.static('./public'))

The app.use() method omits the first parameter, and the user does not need to start with /public/. Instead, the user can directly access the corresponding resource with the address of the corresponding file in the exposed folder

let express = require('express')
let app = express()
app.use(express.static('./public'))
app.listen(3000, () = > {console.log('running... ')})Copy the code

  • Method 3:

app.use('/static/', express.static('./public'))

The first argument is an arbitrary file name that we specify. To access a public resource, the user needs a URL that starts with the file name that we specify

let express = require('express')
let app = express()
app.use('/static/', express.static('./public'))
app.listen(3000, () = > {console.log('running... ')})Copy the code

Express combines with the art-Template template engine

preface

You’ve already seen how to use the art-template template engine in Node.js, but you can also use art-template in Express.

  1. Install the appropriate third-party package
npm install --save art-template
npm install --save express-art-template
Copy the code

Note: Express-art-template here relies on the art-template module, so it must be installed with the art-template template.

  1. Introduce into the projectexpress, the configurationexpress-art-templateA template engine
let express = require('express')
// Get the server instance object
let app = express()

// The express-art-Template template engine is configured to have a render method in the response object of the server request event callback handler, which is used to render the template string to return the rendered result.

app.engine('art'.require('express-art-template'))

// Bind the server to the GET request event and add the corresponding processing callback function

app.get('/', (req, res) => {
  res.render('login.art', {
    title: 'Here's the landing page.'})})// Bind the server listening port number to start the server
app.listen(3000, () = > {console.log('running... ')})Copy the code

The app.engine(‘art’, require(‘express-art-template’)) is a required item, which is the key to configure the template engine. Although the art-template module is not introduced in the project, it must be downloaded and installed together because of dependency. The first parameter in app.engine is the extension name of the parsed template character file. Art indicates that the template file must have an. Art extension, which can be changed to the common. Res.render () first argument is the template string file to parse, the extension of the file is the same as the app.engine() first argument, the template file will be found in the views folder of the current parent directory by default, so there is no need to write file path, can not write file path, therefore, It is also good programming practice to keep files related to page views in the views folder. The second parameter is the template configuration object. If you need to change the default location of res.render() to find the template file, for example I want it to find in a folder other than views, you can use the following configuration statement:

app.set('views'.'Alternate file path') // The first argument must be views
Copy the code
  1. See the results after starting the service

After the language

This blog post as to my GitHub Node learning tutorial materials, if you need demo source partners can go to GitHub clone, welcome friends star, your praise is my continuous update of power.

Pubdreamcc original, welcome to reprint!

Warehouse address: Node learn graphic tutorial