# express

## Basic<br>

const express = require('express')

const app = express()\
Add a new route method to handle requests for the path "/". The first argument specifies the path or URL, the next argument is the route handler. Inside the route handler, let's use the response object to send a status code 200 (OK) and text to the client

app.get('/', (request, response, nextHandler) => {

response.status(200).send('Hello from ExpressJS')

})

Finally, use the listen method to accept new connections on port 1337:

app.listen(1337, () => console.log('Web Server running on port 1337'),)

## Initial

create new&#x20;

```
var express = require('express');
var app = express();

app.get('/', function(req, res){
  res.send('hello world');
});

app.listen(3000);
```

## Router :

We can use app.route()

```
app.route('/book')
  .get(function(req, res) {
    res.send('Get a random book');
  })
  .post(function(req, res) {
    res.send('Add a book');
  })
  .put(function(req, res) {
    res.send('Update the book');
  });
```

Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).

```
app.METHOD(PATH, HANDLER);
```

A `router` object is an isolated instance of middleware and routes. You can think of it as a “mini-application,” capable only of performing middleware and routing functions. Every Express application has a built-in app router

CRUD

```
// GET method route
app.get('/', function (req, res) {
  res.send('GET request to the homepage');
});

// POST method route
app.post('/', function (req, res) {
  res.send('POST request to the homepage');
});
```

```
app.put('/user', function(req, res) {
  res.send('Got a PUT request at /user');
});
```

```
app.delete('/user', function(req, res) {
  res.send('Got a DELETE request at /user');
});
```

![](/files/-Li4VKVeJ1Hl1Vg_YIw7)

![](/files/-Li4VWI67jKoLCIJjWM7)

![](/files/-Li4VP-QzeTKtIyzr1Ux)

![](/files/-Li4VRDK73HiqVUIymgN)

Respond to all METHOD to the /user route:

```
app.all('/user', function(req, res) {
  res.send('Got a request at /user');
});
```

```
const express = require('express');
const router = express.Router();

// middleware that is specific to this router
router.use(function timeLog(req, res, next) {
  console.log('request for books - Time: ', Date.now());
  next();
});
// define the home page route
router.get('/', function(req, res) {
  res.send('Books home page');
});
// define the about route
router.get('/about', function(req, res) {
  res.send('About books');
});

module.exports = router;
```

## Advanced Routing <a href="#express-advanced-routing" id="express-advanced-routing"></a>

regular expression

* `[abc]` Matches either an a, b or c character
* `[a-z]` Matches any characters between a and z, including a and z.
* `.` Matches any character other than newline
* `\d` Matches any decimal digit. Equivalent to \[0-9].
* `a?` Matches an `a` character or nothing.
* `a+` Matches one or more consecutive `a` characters.
* `a*` Matches zero or more consecutive `a` characters.

```
app.get('/ab?cd', function(req, res) {
  res.send('ab?cd');
});
```

#### Routing Parameter <a href="#routing-parameter" id="routing-parameter"></a>

```
Route path: /users/:userId/books/:bookId
Request URL: http://localhost:3000/users/34/books/8989
req.params: { "userId": "34", "bookId": "8989" }

app.get('/users/:userId/books/:bookId', function(req, res) {
  res.send(req.params);
});
```

#### Request Body <a href="#request-body" id="request-body"></a>

```
const app = require('express')();
const bodyParser = require('body-parser');

app.use(bodyParser.json()); // for parsing application/json

app.post('/users', function(req, res) {
  res.send(req.body);
});
```

## app.use(\[path], function)]

```
var express = require('express');
var app = express();

// 一个简单的 logger
app.use(function(req, res, next){
  console.log('%s %s', req.method, req.url);
  next();
});

// 响应
app.use(function(req, res, next){
  res.send('Hello World');
});

app.listen(3000);
```

static:, ./public static service

```
app.use(express.static(__dirname + '/public'));
```

## app.all(path, \[callback...], callback)

match all HTTP active

## Handling error

In Express, 404 responses are not the result of an error, so the error-handler middleware will not capture them. This behavior is because a 404 response simply indicates the absence of additional work to do; in other words, Express has executed all middleware functions and routes, and found that none of them responded. All you need to do is add a middleware function at the very bottom of the stack (below all other functions) to handle a 404 response:

```
app.use(function(req, res, next) {
  res.status(404).send("Sorry can't find that!");
});

app.use(function(err, req, res, next) {
  console.error(err.stack);
  res.status(500).send('Something broke!');
});

app.get('/error', function (req, res) {
  throw new Error('BROKEN') // Express will catch this on its own.
})


app.get('/', function (req, res, next) {
  fs.readFile('/file-does-not-exist', function (err, data) {
    if (err) {
      next(err) // Pass errors to Express.
    } else {
      res.send(data)
    }
  })
})
```

## Express Middleware

1. app.use `app.METHOD()`&#x20;
2. router.use `router.METHOD()`&#x20;
3. &#x20;err app.use(function(err, req,res,next))
4. import, third party middleware&#x20;

```
router.use('url',function(req,res,next){next()})
```

```
app.use('url', function(req,res,next){next()})
```

Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next.

* [应用层中间件](https://expressjs.com/zh-cn/guide/using-middleware.html#middleware.application)
* [路由器层中间件](https://expressjs.com/zh-cn/guide/using-middleware.html#middleware.router)
* [错误处理中间件](https://expressjs.com/zh-cn/guide/using-middleware.html#middleware.error-handling)
* [内置中间件](https://expressjs.com/zh-cn/guide/using-middleware.html#middleware.built-in)
* [第三方中间件](https://expressjs.com/zh-cn/guide/using-middleware.html#middleware.third-party)

Middleware functions can perform the following tasks:

* Execute any code.
* Make changes to the request and the response objects.
* End the request-response cycle.
* Call the next middleware function in the stack.

middleware e.g.

function: redirect('/login');

```
app.use(function(req, res, next) {
  if (req.isAuthenticated) {
    next();
  } else {
    res.redirect('/login');
  }
});
```

```
// define the home page route
router.get('/', function(req, res) {
  res.send('Books home page');
});
```

**Example - Error Handling**

You define error-handling middleware in the same way as other middleware, except with four arguments instead of three; specifically with the signature (err, req, res, next):

```
app.use(function(err, req, res, next) {
  console.error(err.stack);
  res.status(500).send('Something broke!');
});
```

### third part :

```
npm install cookie-parser

var cookieParser = require('cookie-parser');
// load the cookie-parsing middleware
app.use(cookieParser());
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gzqsjtu.gitbook.io/workspace/express.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
