Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

create node js api

/*
In your Console:
	npm init
    npm install express --save
*/
const express = require('express')
const app = express()

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(3000)
Comment

how to design an api node js

const express = require('express');
const Joi = require('joi'); //used for validation
const app = express();
app.use(express.json());
 
const books = [
{title: 'Harry Potter', id: 1},
{title: 'Twilight', id: 2},
{title: 'Lorien Legacies', id: 3}
]
 
//READ Request Handlers
app.get('/', (req, res) => {
res.send('Welcome to Edurekas REST API with Node.js Tutorial!!');
});
 
app.get('/api/books', (req,res)=> {
res.send(books);
});
 
app.get('/api/books/:id', (req, res) => {
const book = books.find(c => c.id === parseInt(req.params.id));
 
if (!book) res.status(404).send('<h2 style="font-family: Malgun Gothic; color: darkred;">Ooops... Cant find what you are looking for!</h2>');
res.send(book);
});
 
//CREATE Request Handler
app.post('/api/books', (req, res)=> {
 
const { error } = validateBook(req.body);
if (error){
res.status(400).send(error.details[0].message)
return;
}
const book = {
id: books.length + 1,
title: req.body.title
};
books.push(book);
res.send(book);
});
 
//UPDATE Request Handler
app.put('/api/books/:id', (req, res) => {
const book = books.find(c=> c.id === parseInt(req.params.id));
if (!book) res.status(404).send('<h2 style="font-family: Malgun Gothic; color: darkred;">Not Found!! </h2>');
 
const { error } = validateBook(req.body);
if (error){
res.status(400).send(error.details[0].message);
return;
}
 
book.title = req.body.title;
res.send(book);
});
 
//DELETE Request Handler
app.delete('/api/books/:id', (req, res) => {
 
const book = books.find( c=> c.id === parseInt(req.params.id));
if(!book) res.status(404).send('<h2 style="font-family: Malgun Gothic; color: darkred;"> Not Found!! </h2>');
 
const index = books.indexOf(book);
books.splice(index,1);
 
res.send(book);
});
 
function validateBook(book) {
const schema = {
title: Joi.string().min(3).required()
};
return Joi.validate(book, schema);
 
}
 
//PORT ENVIRONMENT VARIABLE
const port = process.env.PORT || 8080;
app.listen(port, () => console.log(`Listening on port ${port}..`));
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to convert seaconds into hh:mm:ss in javascript 
Javascript :: how to read a csv file in nodejs 
Javascript :: check if an element is there in js 
Javascript :: extends in javascript 
Javascript :: angular hostlistener 
Javascript :: create an element jquery 
Javascript :: downgrade node version 
Javascript :: console shortcut chrome 
Javascript :: javascript classes and how to import them 
Javascript :: get image as blob 
Javascript :: remove repetition 2d array javascript 
Javascript :: mysql json_array_append 
Javascript :: react i18n outside component 
Javascript :: react native vector icons 
Javascript :: js add a tag inside span 
Javascript :: javascript create object key from variable 
Javascript :: text overflow ellipsis two lines react native 
Javascript :: reverse array without using another array js 
Javascript :: javascript detect tab leave 
Javascript :: jshint ignore line 
Javascript :: js map through array and return array of objects 
Javascript :: debouncing 
Javascript :: javascript parse date dd/mm/yyyy hh:mm:ss 
Javascript :: array asociativo multidimensional javascript 
Javascript :: javascript replaceall 
Javascript :: Contact form tutorial next.js 
Javascript :: remove empty option from bootstrap select javascript 
Javascript :: how to convert an object to a list in js 
Javascript :: javascript replace with UpperCase 
Javascript :: round innerhtml up javascript 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =