Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Introduction


Excerpt

A quick over of three ways to create a node.js application


...

Code Block
languagejs
titleindex.js
var express = require("express");
var app = express();

app.listen(3000, () => {
   console.log("Server running on port 3000");
});

When you run this app you will see the message "Cannot GET /".  If you check the developer's tools console output you will see the following error "GET http://localhost:3000/ 404 (Not Found).  You are seeing this message because you have not set up any event handlers (your application has no endpoints)

Setting Request Handlers

A Web Server receives Requests, processes them, and returns a Response.  We need to establish route handling in our application to handle these Requests. 

When developing modern applications we expose what are called ReSTful web services.  These Requests are a GET request that gets data, a POST request that sends data securely, a PUT request that updates data, and a DELETE request that deletes data.

As well as deciding on the Requests, we also need to decide on we will respond with a Response to Requests.

Let's create a simple GET request that says "hello world"

Code Block
languagejs
titleindex.js
app.get("/sayhello", (req, res, next) => 
{
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.send("Hello World");
});