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 :: buffer in javascript 
Javascript :: javascript write to text file 
Javascript :: boucle for in js 
Javascript :: conditional array element js 
Javascript :: stop keyframe animation javascript 
Javascript :: how to check if value is undefines if condition jquery 
Javascript :: find index in array javascript 
Javascript :: dynamodb pagination nodejs 
Javascript :: iframe reload src 
Javascript :: create angular project 
Javascript :: Uncaught TypeError: .replace is not a function 
Javascript :: check user by id discord js 
Javascript :: nodejs emit event from class 
Javascript :: javascript max array 
Javascript :: webpack config minify 
Javascript :: change p text jqwuery 
Javascript :: useeffect only on mount 
Javascript :: get date in specific timezone 
Javascript :: how to display image in react js component 
Javascript :: increased the value of a counter when a button is clicked in javascript 
Javascript :: image preview before upload jquery 
Javascript :: How to get current time zone in javascript 
Javascript :: merge array of objects javascript 
Javascript :: async import javascript 
Javascript :: update object in array if id exists else append javascript 
Javascript :: get the state of a checkbox 
Javascript :: run function then empty it 
Javascript :: download images from http link in react 
Javascript :: Lazy Loading Routes vue 
Javascript :: header disallowed by preflight response in express 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =