简体   繁体   English

NodeJS Express Rest API 教程

[英]NodeJS Express Rest API tutorial

Hello everyone I am new in NodeJS environment .大家好,我是 NodeJS 环境的新手。 I want to develop a API based application using NodeJS, MongoDB and Express JS.我想使用 NodeJS、MongoDB 和 Express JS 开发基于 API 的应用程序。 I need a complete api development tutorial where I learn route, middleware , mongoose one to many and many to many feature and nodeJS login system .我需要一个完整的 api 开发教程,在那里我学习路由、中间件、mongoose 一对多和多对多功能和 nodeJS 登录系统。 Basically I need a complete package of tutorial about NodeJS rest api development基本上我需要一个关于 NodeJS rest api 开发的完整教程包

I would recommend checking out the Reddit API Clone by Dev Coffe and the Nodejs tutorials from LearnCode.academy .我会建议您检查出由reddit的API克隆开发咖啡,并从教程的NodeJS LearnCode.academy

There are also several paid courses on Udemy and Treehouse . UdemyTreehouse上还有几门付费课程。

Here is a sample service.Create a file structure in a suitable way.这是一个示例服务。以合适的方式创建文件结构。 Code given below is a code of complete service.下面给出的代码是完整服务的代码。 Also refer express documentations for furthur knowledge另请参阅快速文档以获取更多知识

server.js服务器.js

const express=require('express');
const bodyparser=require('body-parser');
const cors=require('cors');
const routing=require('./routes');
const app=express();

app.use(cors());
app.use(bodyparser.json());
app.use('/',routing);

app.listen(8095,'localhost',err=>{
    if(err){
        console.log(err);
        process.exit(-1);
    }
   console.log('connected to localhost at post 8095');
});

routes.js路由.js

const express=require('express');
const routes=express.Router();
const bookroute=require('./Books/controllers/book.controller');
const authorroute=require('./Books/controllers/author.controller');

routes.use('/books',bookroute);
routes.use('/authors',authorroute);

module.exports=routes;

controllers->author.controller控制器->作者.控制器

const express=require('express');
const router=express.Router();
const authorModel=require('../models/author.model');

router.get('/',authorModel.getAuthors);
module.exports=router;

controllers->book.controller控制器->book.controller

const express=require('express');
const router=express.Router();
const bookModel=require('../models/book.model');

router.get('/',bookModel.getBooks);
router.get('/authors',bookModel.getAutBooks);
router.get('/:name',bookModel.getBook);
router.post('/',bookModel.addBooks);
router.delete('/:name',bookModel.deleteBooks);

module.exports=router;

models->author.model模型->作者.模型

var schema=require('../dbSchema');
var authormodel=schema.model('author');


exports.getAuthors = function (req,res) {
    authormodel.find().exec().then((data)=>{
        res.send(data)
    })
};

models->book.model模型->book.model

var schema=require('../dbSchema');
var bookmodel=schema.model('book');

exports.getBooks = function (req,res) {
    bookmodel.find().exec().then((data)=>{
        res.send(data)
    }).catch((err)=>{
        console.log(err);
    });
};

exports.getBook = function (req,res) {
    var bkName=req.params.Author;
    bookmodel.find({Name:bkName}).exec().then((data)=>{
       res.send(data)
    }).catch((err)=>{
       console.log(err);
   }   );
 };

exports.getAutBooks = function (req,res) {
bookmodel.find({},'Author').then((data)=>{
     res.send(data);
}).catch((err)=>{
    console.log(err);
});
};

   exports.deleteBooks=function(req,res){
   var bkName=req.params.name;
   bookmodel.remove({Name:bkName}).exec().then((data)=>{
      res.status(200);
      console.log(bkName);
   }).catch((err)=>{
    console.log(err);
});
};

exports.addBooks=function(req,res){
var newBook=new bookmodel({
    Name:req.body.Name,
    ISBN:req.body.ISBN,
    Author:req.body.Author,
    Price:req.body.Price,
    Year:req.body.Year,
    Publisher:req.body.Publisher
});
newBook.save().then(()=>{
    res.status(201);
}).catch((err)=>{
    console.log(err);
});
};

dbchema数据库架构

const mongoose=require('mongoose');
const schema=mongoose.Schema;

const bookSchema=new schema({
    Name:{type:String,required:true},
    ISBN:{type:String,required:true},
    Author:{type:String,required:true},
    Price:{type:String,required:true},
    Year:{type:String,required:true},
    Publisher:{type:String,required:true}

});

const authorSchema=new schema({
    fname:{type:String,required:true},
    lname:{type:String,required:true},
    nationality:{type:String,required:true}
});

mongoose.model('book',bookSchema,'booklist');
mongoose.model('author',authorSchema,'authorlist');

mongoose.connect('mongodb://localhost:27017/dbFinal',(err)=>{
    if(err){
        console.log(err);
        process.exit(-1);
    }
});

   module.exports=mongoose;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM