Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Last active May 11, 2020 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azamsharp/4449ca048872a8c28a38f86930f6da2b to your computer and use it in GitHub Desktop.
Save azamsharp/4449ca048872a8c28a38f86930f6da2b to your computer and use it in GitHub Desktop.
// express package is used to start the server
const express = require('express')
const app = express()
// create routes/endpoints
// localhost = 127.0.0.1
// Port = 3000
// /hello route/endpoint
// http://localhost:3000/hello
// req = Request coming from a client (browser)
// res = Response from the server
app.get('/hello',function(req,res) {
// res.send("Hello World!") This returns String
// return JSON
res.json({name: "Mary Doe"})
//res.json({firstname: "John", lastname: "Doe"})
})
app.get('/customers',function(req,res) {
let customers = []
customers.push({name: "Alex"})
customers.push({name: "Mary"})
customers.push({name: "John"})
res.json(customers)
})
// /movies/action
// /movies/kids
// /movies/fiction
// /movies/drama
// /movies/cat also matches
// /movies/fiction
// /movies/year/2019/genre/comedy
// Amazon - www.amazon.com/books/products
// www.amazon.com/toys/products
// www.amazon.com/plants/products
app.get('/movies/year/:year/genre/:genre',function(req,res) {
// access the value of the :genre route parameter
let movieGenre = req.params.genre
let year = req.params.year
console.log(movieGenre)
let movies = []
movies.push({name: 'Batman', year: "2020", genre: "action"})
movies.push({name: 'Superman', year: "2019", genre: "action"})
movies.push({name: 'Spiderman', year: "2018", genre: "action"})
movies.push({name: 'Finding Nemo', year: "2018", genre: "kids"})
movies.push({name: 'Finding Dori', year: "2018", genre: "kids"})
movies.push({name: 'Mickey Mouse', year: "2018", genre: "kids"})
res.json(movies)
})
// starting the server
app.listen(3000,function() {
console.log("Server is running")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment