Installing Express and Making it Easier to Build a Web Server

Why use express

  • You have access to express request and response object, http methods
  • No need to set send body and send content type express will figure this out for you.

Big Word

Environment Variables: Global variables specific to the environment (server) our code is living in.

Different servers can have different variable settings, and we can access those values in code.

  • var port = process.env.PORT || 3000;

HTTP Method: specifies the type of action the request wishes to make

GET, POST, DELETE, and others. Also called verbs.

The Code

var express = require('express');
var app = express();

var port = process.env.PORT || 3000;

//this is get method

//this is what gets return if the client request for / directory
app.get('/', function(req, res) {
    res.send('<html><head></head><body><h1>Hello world!</h1></body></html>');
});

//what gets return when client request for /api directory
app.get('/api', function(req, res) {
    res.json({ firstname: 'John', lastname: 'Doe' });
});

app.listen(port);

results matching ""

    No results matching ""