1. handling forms in express
    1. <form method="POST" action="/submit-form">
    2. <input type="text" name="username" />
    3. <input type="submit" />
    4. </form>

    When the user press the submit button , the browser will automatically make a POST request to the /submit-form URL on the same origin of the page, sending the data it contains, encoded as application/x-www-form-urlencoded . In this case, the form data contains the usename input field value.

    To extract it, you will use the express.urlencoded() middleware, provided by Express:

    1. const express = require('express')
    2. const app = express()
    3. app.use(express.urlencoded())

    Now you need to create a POST endpoint on the /submit-form route, and any data will be available on Request.body :

    1. app.post('/submit-form', (req, res) => {
    2. const username = req.body.username;
    3. res.end()
    4. })

    Don’t forget to validate the data before using it, using express-validator .

    1. Node, the difference between development and production

    You can have different configurations for production and development environments.

    Node assumes it’s always running in a development environment. You can signal Node.js that you are running in production by setting the NODE_ENV=production environment variable.

    This is usually done by executing the common

    1. export NODE_ENV=production

    Setting the environment to production generally ensures that

    • logging is kept to a minimum, essential level

    • more caching levels take place to optimize performance.

    Express provides configurations hooks specific to the environment, which are automatically called based on the NODE_ENV variable value:

    1. app.configure('development', () => {
    2. // ...
    3. })
    4. app.configure('production', () => {
    5. // ...
    6. })
    7. app.configure('production', 'staging', () => {
    8. // ...
    9. })

    For example you can use this to set different error handlers for different mode:

    1. app.configure('development', () => {
    2. app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
    3. });
    4. app.configure('production', () =>{
    5. app.use(express.errorHandler());
    6. });
    1. Manage cookies with express

    Use the Response.cookie() method to manipulate your cookies.

    Examples:

    1. res.cookie('username', 'Flavio')

    This method accepts a third parameter which contains various options:

    1. res.cookie('username', 'Flavio', {
    2. domain: '.yuque.com',
    3. path: '/administrator',
    4. secure: true
    5. });
    6. res.cookie('username', 'Flavio', {
    7. expires: new Date(Date.now() + 900000),
    8. httpOnly: true,
    9. });

    The most useful parameters you can set are:

    Value Description
    domain the cookie domain name
    expires set the cookie expiration date. If missing, or 0, the cookie is a session cookie.
    httpOnly set the cookie to be accessible only by the web server. see HttpOnly
    maxAge set the expiry time relative to the current time, expressed in milliseconds.
    path the cookie path. Defaults to /
    secure Marks the cookie HTTPS only
    signed set the cookie to be signed
    sameSite Value of SameSite

    A cookie can be cleared with

    1. res.clearCookie('username')