Express

Express is a Javascript-based server application that will help route requests from the client.

Getting Started

First, start your project with npm init in the project folder. Then npm i express. Now you can start implementing the barebones in your project.

// app.js
const express = require('express');
const app = express();
const port = 3000;

app.listen(port, () => console.log(`Listening on port ${port}`));

To get it running, set up your package.json with a script to run. You can also use nodemon to run and restart upon file changes.

"scripts": {
  "start": "node app.js",
  "dev": "nodemon app.js"
}

Upon running one of these scripts (npm run start/dev), you should see a message in your terminal that your app is listening on port 3000.

Routes

// Parameters: `route`, and `callback` when request and response objects are received and created
app.get('/', (req, res) => {
  const data = 'We did it!';
  res.send(data);
});

Upon completing this and restarting your server, you should be able to go to localhost:3000 and see "We did it!" in your browser window.

Passing Data

When receiving data on the server side from the client, you used to have to install body-parser, but Express has since included it in their core. To ensure that Express parses your incoming data, use the following:

app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded

After implementing this on the server side, you can access the data via req.body.

Query Parameters

You can access the query parameters via the req.query object.

// .../products?example=1?another=five
app.get('/products', (req, res) => {
  const example = req.query.example;
  const another = req.query.another;
  res.send(JSON.stringify([example, another]));
});

References

  1. http://expressjs.com/en/starter/installing.html
  2. https://stackoverflow.com/questions/17007997/how-to-access-the-get-parameters-after-in-express
  3. https://expressjs.com/en/starter/static-files.html
  4. https://expressjs.com/en/4x/api.html#req
Incoming Links

Last modified: 202401040446