json 使用Express JS的基本RESTful API

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json 使用Express JS的基本RESTful API相关的知识,希望对你有一定的参考价值。

const config = require('config');
const Joi = require('joi');
const genres = require('./routes/genres');
const express = require('express');

const app = express();
app.use(express.json());
app.use('/api/genres', genres);

console.log('Name of App : '+config.get('name'))
console.log('Name of Mail : '+config.get('mail.host'))
console.log('Name of Password : '+config.get('mail.password'))
app.get('/', (req, res) => {
    res.send('<h1>Hi There, Welcome to Rest API</h1>');
});
// Running Server on Port 3000
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Server is listening on port ${port}`));
const express = require('express');
const router = express.Router();


var movieGenres = [
    { id: 1, name: 'Sci-Fi' },
    { id: 2, name: 'Romantic' },
    { id: 3, name: 'Drama' },
    { id: 4, name: 'Action' },
    { id: 5, name: 'Family' }
];


// Get All Movie Genre
router.get('/', (req, res) => {
    res.send(movieGenres);
});

// Search and Get the movie genre by ID 
router.get('/:id', (req, res) => {
    const genre = movieGenres.find(genre => genre.id === parseInt(req.params.id));
    if(!genre) return res.status(404).send('Movie Genre with that ID was NOT FOUND');
    res.send(genre);
});

// Inserting new Movie Generes into the Array 
router.post('/', (req, res)=>{
    const schema = {
        name: Joi.string().min(3).required()
    };
    const result = Joi.validate(req.body, schema);
    if(result.error){
        res.send(result.error.details[0].message)
        return;
    }
    var movieGenre = {
        id: movieGenres.length+1,
        name: req.body.name
    };
    movieGenres.push(movieGenre);
    res.send(movieGenre);
});
// Updating value of Movie Genre
router.put('/:id', (req, res)=>{
    const genre = movieGenres.find(genre => genre.id === parseInt(req.params.id));
    if(!genre) return res.status(404).send('Movie Genre with that ID was NOT FOUND');
    const schema = {
        name: Joi.string().min(3).required()
    };
    const result = Joi.validate(req.body, schema);
    if(result.error){
        res.send(result.error.details[0].message)
        return;
    }
    genre.name= req.body.name;
    res.send(genre);
});

// Delete movie genre from the array
router.delete('/:id', (req, res)=>{
    const genre = movieGenres.find(genre => genre.id === parseInt(req.params.id));
    if(!genre) return res.status(404).send('Movie Genre with that ID was NOT FOUND');

    const index = movieGenres.indexOf(genre);
    movieGenres.splice(index, 1);
    res.send(genre);
});


module.exports = router;
{
  "name": "express-api",
  "version": "1.0.0",
  "description": "Developing rest api using node js with express framework",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Hasib",
  "license": "ISC",
  "dependencies": {
    "config": "^3.0.0",
    "express": "^4.16.4",
    "joi": "^14.3.0"
  }
}

以上是关于json 使用Express JS的基本RESTful API的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Express JS/Railway JS (Node.JS) 生成 JSON

使用 Express 4 在 Node.js 中解析 JSON 发布请求

使用 Node.js 和 Express 将 JSON 数据集显示为表格

Express.js 中的 POST 数据 JSON 验证

Ajax的基本使用(以express框架为例)

在单个 Node.js + Express 响应中发送图像和 JSON 数据