Skip to content

Instantly share code, notes, and snippets.

@gregfenton
Forked from acidtone/README.md
Last active March 10, 2023 03:33
Show Gist options
  • Save gregfenton/f08de3ed9027cbe880891223df287f89 to your computer and use it in GitHub Desktop.
Save gregfenton/f08de3ed9027cbe880891223df287f89 to your computer and use it in GitHub Desktop.
Express: Hello World!

Express: Add "Hello World!" API

Prerequisites

  1. Initialized ExpressJS project as per this gist

Example directory structure

project-root
    ├── node_modules
    ├── package-lock.json
    ├── package.json
    └── server.js

Instructions

  1. In your favourite editor (VSCode), open the file server.js

  2. At the bottom of the file, add endpoint (an API!) handler for GET / requests (i.e. the "home" of our ExpressJS server):

    app.get('/', (request, response) => {
      response.send('Hello World!')
    })
  3. Open a browser and go to the address: http://localhost:4000/

    • you should see 'Hello World!' displayed

Related Gists

{
"name": "hello-express",
"version": "1.0.0",
"description": "An introductory express app",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Tony Grimes",
"license": "MIT",
"dependencies": {
"express": "^4.17.1"
}
}
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.listen(PORT, function() {
console.log(`Example app listening at http://localhost:${PORT}`);
})
app.get('/', (request, response) => {
response.send("Hello World!");
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment