1 of 11

Express.JS

CSE 3330 - SMU - Spring 2019

2 of 11

Express.JS

  • https://expressjs.com/
  • “Fast, unopinionated, minimalist web framework for Node.JS”
  • E of MEAN stack and MERN stack
  • Alternatives include Hapi.JS and Koa.JS

3 of 11

npm

  • Node Package Manager
  • Installed with Node.js (or should have been…)
  • Manage Node packages that you need for a particular project
  • Can work in conjunction with package.json file as part of a Node project
  • npm init
    • initializes a new a new project
    • ask some questions and creates a basic package.json file for you.

4 of 11

package.json

> npm init

This utility will walk you through creating a package.json file.

It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields

and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and

save it as a dependency in the package.json file.

Press ^C at any time to quit.

package name: (npmexample) firstnpm

version: (1.0.0) 0.0.1

description: A first use of npm

entry point: (index.js) app.js

test command:

git repository:

keywords:

author: Mark Fontenot

license: (ISC)

5 of 11

package.json

{

"name": "firstnpm",

"version": "0.0.1",

"description": "A first use of npm",

"main": "app.js",

"scripts": {

"test": "echo \"Error: no test specified\" && exit 1"

},

"author": "Mark Fontenot",

"license": "ISC"

}

6 of 11

package.json

npm install express

npm install -g nodemon (may need to sudo)

{

"name": "firstnpm",

"version": "0.0.1",

"description": "A first use of npm",

"main": "index.js",

"scripts": {

"test": "echo \"Error: no test specified\" && exit 1"

},

"author": "Mark Fontenot",

"license": "ISC",

"dependencies": {

"express": "^4.16.4"

}

}

^ Most recent major version (4.x.x here)

~ Most recent minor version (4.16.x here)

7 of 11

app.js

const express = require('express');

const app = express();

const port = 3000;

app.get('/', (req, res) => {

res.send('Hello World');

});

app.listen(port, () => {

console.log('Simple Example');

});

Handler for GET /

Start the server

8 of 11

app.js

const express = require('express');

const app = express();

const port = 3000;

app.get('/', (req, res) => {

res.send('Hello World');

});

app.listen(port, () => {

console.log('Simple Example');

});

1. Type the code below into app.js.

2. execute node app.js from the command line.

3. In a browser, go to http://localhost:3000/.

4. Add a command that will print to the console “Incoming request” in the GET / event handler.

5. Re-execute the server

9 of 11

nodemon

nodemon app.js

Watches for changes to files and will automatically reload the file

Try it out by responding to the client with

<h1>Hello World</h1>

10 of 11

Express Routing

Basic format:

app.HTTPMethod(Path, Handler);

HTTPMethod can be one of a number of HTTP verbs.

Common ones: get, post, put, delete

11 of 11

Examples

app.get('/', function (req, res) {� res.send('Hello World!')�})

app.post('/', function (req, res) {

res.send('Got a POST request')

})

app.put('/user', function (req, res) {

res.send('Got a PUT request at /user')

})

app.delete('/user', function (req, res) {

res.send('Got a DELETE request at /user')

})