Express

Updated: 2020-05-07

Create a folder

$ mkdir app
$ cd app

init

$ npm init

Install express

$ npm install express --save

Create app.js file:

const express = require("express");
const app = express();
const port = 3000;

app.get("/", (req, res) => res.send("Hello World!\n"));

app.listen(port, () =>
  console.log(`Example app listening at http://localhost:${port}`)
);

Run the script

$ node app.js
Example app listening at http://localhost:3000

From a different terminal:

$ curl localhost:3000
Hello World!

or open your browser and enter http://localhost:3000/

Read data from database:

const { Client } = require("pg");
const client = new Client();
client.connect();

app.get("/", async (req, res) => {
  const result = await client.query("SELECT * FROM civilizations");
  res.send(result);
});