Setup is super easy. Clone the repository -
git clone https://github.com/sunilksamanta/node-mongoose-setup
cd node-mongoose-setup
npm install
Create an .env
file at the root of your project with the following.
MONGO_URL=YOUR_MONGO_URL
PORT=5000[YOUR_DESIRED_PORT]
NODE_ENV=YOUR_APP_ENVIRONMENT[production/development]
JWT_SECRET=YOUR_JWT_SECRET_STRING
An example file .env.example
is included.
Your project is ready. Now start the project.
npm start
Go to http://localhost:5000
. You should see a default welcome page.
Your API base path is http://localhost:5000/api
.
First create some accounts to get started with the authentication.
JWT authentication is added in this project. User model is defined in models/User.js.For Register, Login, Logout use these urls —
[POST] api/auth/register
[POST] api/auth/login
[GET] api/auth/logout
Controller, Model & Service oriented architecture
Auth with JWT & Db Store
Async/Await support
User Module
Post Module (Sample CRUD)
Media Upload
Centralized Http Response
Error Handler
.env support
Multi Environment config setup
Autobind Methods
Built in Pagination
├─ .env
├─ .gitignore
├─ config
│ ├─ config.js
│ ├─ database.js
│ ├─ routes.js
│ └─ server.js
├─ index.js
├─ package.json
├─ system
└─ src
├─ controllers
│ ├─ AuthController.js
│ ├─ MediaController.js
│ └─ PostController.js
├─ helpers
├─ models
│ ├─ Auth.js
│ ├─ Media.js
│ ├─ Post.js
│ └─ User.js
├─ routes
│ ├─ auth.js
│ ├─ media.js
│ └─ post.js
└─ services
├─ AuthService.js
├─ MediaService.js
├─ PostService.js
└─ UserService.js
We have 2 base classes — One for Controller and another for Service.
This base controller have the basic CRUD operations. To create a new controller just extend this base Controller class.
This is the base Service class which includes the database operations. If you want to change the default behaviour of the services you can update this file.
If you want to create a new Module say Post. Then you’ll have to create 4 basic files. One Controller, one Service, one Model and one route file. And add the new route in routes/index.js with desired url.There is a “Post” CRUD module included in this project for example.
controllers/PostController.js
models/Post.js
services/PostService.js
routes/post.js
As an example if you see in the media Controller — the default delete method is overriden by its own class method as we have to delete the file from the file system also. So the overriden method is like bellow —
async delete(req, res, next) {
const { id } = req.params;
try {
const response = await this.service.delete(id);
// File Unlinking..
if (response.data.path) {
console.log("unlink item", response.data.path);
fs.unlink(response.data.path, function (err) {
if (err) {
console.log("error deleting file");
throw err;
}
console.log("File deleted!");
});
}
return res.status(response.statusCode).json(response);
}
catch (e) {
next(e);
}
}
You can reply to this article REST API Structure using NodeJS MongoDB (Mongoose)
If you have any suggestion, feedback or you want to make this project more powerful — feel free to report issues or request a feature or suggest some changes.
Read the Contributing guideline.
This project is licensed under the terms of the MIT license.
Special thanks to @thisk8brd for the concept of this API Structure.
1.安装依赖 yarn global add @nestjs/cli或npm i -g @nestjs/cli 2.新建项目 nest new 文件名 3.运行项目 nest start nest start watch 热更新 结构 main-appmodules---appController ---appservice 4.swargger 官网地址
原文地址:http://blog.gdfengshuo.com/2017/07/29/20 前言 Express 是基于 Node.js 平台的 web 应用开发框架,在学习了 Node.js 的基础知识后,可以使用 Express 框架来搭建一个 web 应用,实现对数据库的增删查改。 数据库选择 MongoDB,它是一个基于分布式文件存储的开源数据库系统,Mongoose 是 MongoDB
0.前言 架构:前端(Frontend)、后端(Backend) 前端工程化环境:webpack CSS预处理工具:sass JS库(Ajax):jQuery SPA(single page application),路由:SME-Router JS模块化:ES Module,CommonJS Module UI组件库:Bootstrap(AdminLTE) RMVC:art-template N
by Nick Karnik 尼克·卡尼克(Nick Karnik) MongoDB的Mongoose简介 (Introduction to Mongoose for MongoDB) Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. It manages relationships between
说明 使用node.js搭建简单的网站。 这是鄙人第一次做网站,网站逻辑也非常简单,主要是根据黑马程序员的公开课程制作的,但是进行了小范围的逻辑修改。 本文主要介绍后端逻辑。 数据库:mongoDB node.js web应用框架:Express 前端框架:Bootstrap 环境搭建 安装node.js , 使用express-generator生成器创建脚手架(当然也可以不用脚手架) 入口文件
最近学习React 自己的在线购物网站做的差不多了,需要开始用户登陆,生成订单,然后用palpay付款了。于是开始自己搭建一个Node.js 的后端服务器。 安装node.js mongodb 就不再说了,直接开始从代码开始说起吧 package.json 可以看出来要安装的包: 使用了以下包: express Web框架 mongoose 数据库连接库 morgan morgan是expres
Node Express Mongoose A boilerplate application for building web apps using express, mongoose and passport. Read the wiki to understand how the application is structured. Usage git clone https://githu
Nodejs Express Mongoose Demo This is a demo application illustrating various features used in everyday web development, with a fine touch of best practices. The demo app is a blog application where us
我正在建设的网站上有一个带有评论功能的页面。该网站类似于Yelp for campsites,mongo db集合中的每个露营地都有一个字段-comments-用于存储该露营地上发布的每条评论的id,该id引用另一个名为comments的集合中的对象。添加、编辑、查看和删除评论除了删除评论外,该评论的id不会从其关联营地的评论数组中删除。 以下是一个目前营地记录的例子: 如您所见,营地对象当前引用
Mongoose 是设计用于异步环境的 MongoDB 对象模型工具,支持 promises 和 callbacks。 概述 连接到 MongoDB 首先需要定义一个连接。如果应用仅使用一个数据库,则使用mongoose.connect。如果需要创建其他连接,请使用mongoose.createConnection。 connect 和 createConnection都使用 mongodb://
问题内容: 据我所知,方法是,那是,并且也喜欢,但它们不是存储在数据库中。 但是,我想知道那是和之间的唯一区别。还有其他我想念的东西吗? 问题答案: 实例方法,静态方法或虚拟方法均未存储在数据库中。方法与虚拟函数之间的区别在于,虚拟函数的访问方式类似于属性,方法的调用方式类似于函数。实例与静态实例与虚拟实例之间没有区别,因为在类上具有可访问的虚拟静态属性是没有意义的,但在类上具有某些静态实用程序或
Mongoose Web Server是一款易于使用的Web服务器,它可以嵌入到其它应用程序中,为其提供Web接口。 主要特写: 跨平台,支持 Windows、OS X 和 Linux 支持 CGI, SSL, SSI, Digest (MD5) 认证,WebSocket 和 WebDAV 支持断点续传和 URL 重写 基于 IP 的 ACL,支持 Windows 服务,支持 GET, POST,