Skip to content

Instantly share code, notes, and snippets.

@awhit012
Created July 23, 2019 21:33
Show Gist options
  • Save awhit012/3d63a8df4c539f64b7ca399aae69dfbf to your computer and use it in GitHub Desktop.
Save awhit012/3d63a8df4c539f64b7ca399aae69dfbf to your computer and use it in GitHub Desktop.
const http = require('http')
const books = [
{id: 1, title: "You Don't Know JS", author: "Kyle Simpson"},
{id: 2, title: "Sometimes a Great Notion", author: "Ken Kesey"},
{id: 3, title: "The Teachings of Don Juan", author: "Carlos Casteneda"}
]
// check the url, if it is /books or /books/:id
// check the method, if it is GET, POST, PUT, or DELETE
// depending on the above, we want to send back our response.
const makeServer = (request,response) => {
if(request.url === "/books") {
console.log("it was /books")
if(request.method === "GET") {
response.writeHead(200,{'Content-Type':'application/json'});
response.write(JSON.stringify(books));
response.end();
}
if(request.method === "POST") {
let body = [];
request.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
let book = Buffer.concat(body).toString();
books.push(book)
response.writeHead(200,{'Content-Type':'application/json'});
response.write(JSON.stringify(books));
response.end();
});
}
} else {
let bookId = parseInt(request.url.slice(7))
if(bookId) {
if(request.method === "GET") {
let foundBook;
books.forEach( book => {
if(book.id === bookId) {
foundBook = book;
}
})
if (foundBook) {
response.writeHead(200,{'Content-Type':'application/json'});
response.write(JSON.stringify(foundBook));
response.end();
} else {
response.writeHead(404,{'Content-Type':'application/json'});
response.write("Not found");
response.end();
}
} else if (request.method === "DELETE") {
let foundBook;
let index;
books.forEach( (book, idx) => {
if(book.id === bookId) {
foundBook = book;
index = idx;
}
})
if (foundBook) {
books.splice(index, 1)
response.writeHead(200,{'Content-Type':'application/json'});
response.end();
} else {
response.writeHead(404,{'Content-Type':'application/json'});
response.write("Not found");
response.end();
}
} else if (request.method === "PUT" || request.method === "PATCH") {
let body = [];
request.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
let newBookData = Buffer.concat(body).toString();
});
let foundBook;
books.forEach( (book, idx) => {
if(book.id === bookId) {
foundBook = book;
}
})
if (foundBook) {
for(key in newBookData) {
foundBook[key] = newBookData[key]
}
response.writeHead(200,{'Content-Type':'application/json'});
response.write(JSON.stringify(foundBook));
response.end();
} else {
response.writeHead(404,{'Content-Type':'application/json'});
response.write("Not found");
response.end();
}
}
//deal with get one, edit, and delete
} else {
response.writeHead(404,{'Content-Type':'application/json'});
response.write("Not found");
response.end();
}
}
}
const server = http.createServer(makeServer);
server.listen(3000,()=>{
console.log('Node server created at port 3000')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment