Getting Started

Response

app.get('/', function (req, res) {
  console.dir(res.headersSent) //false
  res.send('OK')
console.dir(res.headersSent) //true
})

#Attribute

:- :-
res.app #
res.headersSent #
res.locals #

#Method

:- :-
res.append() #
res.attachment() #
res.cookie() #
res.clearCookie() #
res.download() Prompt for files to download #
res.end() end the response process #
res.format() #
res.get() #
res.json() Send JSON response #
res.jsonp() Send a response with JSONP support #
res.links() #
res.location() #
res.redirect() Redirect request #
res.render() render view template #
res.send() Send various types of responses #
res.sendFile() Send a file as an octet stream #
res.sendStatus() #
res.set() #
res.status() #
res.type() #
res.vary() #

Request

#Attribute

:- :-
req.app #
req.baseUrl #
req.body #
req.cookies #
req.fresh #
req.hostname #
req.ip #
req.ips #
req.method #
req.originalUrl #
req.params #
req.path #
req.protocol #
req.query #
req.route #
req.secure #
req.signedCookies #
req.stale #
req.subdomains #
req.xhr #

#Method

:- :-
req.accepts() #
req.acceptsCharsets() #
req.acceptsEncodings() #
req.acceptsLanguages() #
req.get() Get HTTP request header fields #
req.is() #
req.param() #
req.range() #

Application

var express = require('express')
var app = express()
console.dir(app.locals.title)
//=> 'My App'
console.dir(app.locals.email)
//=> '[[email protected]](/cdn-cgi/l/email-protection)'

#Attribute

:- :-
app.locals Local variables in the application #
app.mountpath Path pattern for mounting sub-apps #

#Events

:- :-
mount The child application is mounted on the parent application, and the event is triggered on the child application #

#Method

:- :-
app.all() #
app.delete() #
app.disable() #
app.disabled() #
app.enable() #
app.enabled() #
app.engine() #
app.get(name) #
app.get(path, callback) #
app.listen() #
app.METHOD() #
app.param() #
app.path() #
app.post() #
app.put() #
app.render() #
app.route() #
app.set() #
app.use() #

Router

:- :-
router.all() #
router.METHOD() #
router.param() #
router.route() #
router.use() #

express()

:- :-
express.json() #
express.raw() #
express.Router() #
express.static() #
express.text() #
express.urlencoded() #

express -h

Usage: express [options] [dir]
Options:
  -h, --help output usage information
      --version output version number
  -e, --ejs add ejs engine support
      --hbs add hbs engine support
      --pug add pug engine support
  -H, --hogan add hogan.js engine support
      --no-view No view engine generated
  -v, --view <engine> add view <engine> support (ejs|hbs|hjs|jade|pug|twig|vash) (default jade)
  -c, --css <engine> add stylesheet <engine> support (less|stylus|compass|sass) (default css)
      --git add .gitignore
  -f, --force force non-empty directories

Create a myapp project

$ express --view=pug myapp
# run the application
$ DEBUG=myapp:*npm start

Hello World

  • "Create project, add package.json configuration
$ mkdir myapp # create directory
$ cd myapp    # enter the directory
$ npm init -y # Initialize a configuration
  • Install dependencies
$ npm install express
  • Entry file index.js add code:
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
  res. send('Hello World!')
})
app. listen(port, () => {
  console.log(`Listening port on ${port}`)
})
  • Run the application using the following command
$ node index.js
Comments