Building a REST API

Navigate to the repo location in terminal:

cd AutomationDatatools/distillery/

Start the server

DEBUG=distillery ./bin/www

Go to your web browser and navigate to the localhost

http://localhost:3000/api/imsreports

The structure looks like this for public, routes and views:

|– public
|   `– stylesheets
|       `– style.css
|– routes
|   |– api
|   |   |– imsreports.js
|   |   `– index.js
|   `– index.js
`– views
|– api
|   `– index.ejs
|– error.ejs
|– index.ejs
`– main.ejs

Advertisements

Code School Real Time Web with Node 5.3-5.4

This code will need some review. It works, but I want to understand the second objective more:

Now, use the name parameter from the URL to retrieve a quote from the quotes object and write it out to the response. Note: No piping here, just write the quote string to the response like you did in previous levels (and then close the response).

var express = require(‘express’);
var app = express();

var quotes = {
‘einstein’: ‘Life is like riding a bicycle. To keep your balance you must keep moving’,
‘berners-lee’: ‘The Web does not just connect machines, it connects people’,
‘crockford’: ‘The good thing about reinventing the wheel is that you can get a round one’,
‘hofstadter’: ‘Which statement seems more true: (1) I have a brain. (2) I am a brain.’
};

app.get(‘/quotes/:name’,function(req, res){
res.end(quotes[req.params.name]);
});

app.listen(8080);

Then the next one added on. Here’s the final code.

var express = require(‘express’);
var app = express();

var quotes = {
‘einstein’: ‘Life is like riding a bicycle. To keep your balance you must keep moving’,
‘berners-lee’: ‘The Web does not just connect machines, it connects people’,
‘crockford’: ‘The good thing about reinventing the wheel is that you can get a round one’,
‘hofstadter’: ‘Which statement seems more true: (1) I have a brain. (2) I am a brain.’
};

app.get(‘/quotes/:name’, function(req, res) {
var quote = quotes[req.params.name];
res.render(‘quote.ejs’, {
name: req.params.name,
quote: quote
});
});

app.listen(8080);