GithubHelp home page GithubHelp logo

makinhs / rest-api-tutorial Goto Github PK

View Code? Open in Web Editor NEW
424.0 21.0 300.0 367 KB

This is a sample source code for the article published on Toptal: https://www.toptal.com/nodejs/secure-rest-api-in-nodejs

JavaScript 98.23% Dockerfile 1.77%
expressjs expressjs-api rest-api

rest-api-tutorial's Introduction

REST API Tutorial

This sample is published as part of the corresponding article at the Toptal Engineering Blog. Visit https://www.toptal.com/developers/blog and subscribe to our newsletter to read great posts!

Before using

  • Please make sure that you have:
  • Node.js installed (https://nodejs.org/)
  • MongoDB installed and running locally (https://www.mongodb.com/)
    • Using Windows, just open the terminal at where you installed mongo and run mongod.exe
  • Run npm install or yarn in your root project folder

Usage

To run the project, please use a command line the following:

  • npm start
    • It will run the server at port 3600.

2019-09-13 update

  • Refactored mongoose to a proper common service.
  • Added a Dockerfile and docker-compose configuration.

If you are familiar to docker and you have docker installed on your machine and just want to run the project without issues please do:

  • docker-compose build
  • docker-compose up
  • It will run the mongodb at port 27017 (for testing purposes only).
  • It will run the server at port 3600.

2020-02-01

I've created a 2020 version of this project using Typescript. If you might be interested on it, please check the following repository: https://github.com/makinhs/expressjs-api-tutorial

2020-09-09

  • Updated and pruned dependencies.
  • Fixed deprecation warnings.
  • Leveraged findOneAndUpdate to simplify PATCH code.
  • Changed default MongoDB server name to localhost to simplify first-time setup.
  • Checked that it works with the latest version of Node.js, 14.9.0.

2020-11-14

rest-api-tutorial's People

Contributors

codingthat avatar haflinger avatar jopchumba avatar kuntalakrishna avatar makinhs avatar mcmoodoo avatar mwager avatar tylerbyte666 avatar vusimoy0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rest-api-tutorial's Issues

body-parser is Deprecated

Deprecated in 2014!
Express has this built in. I can make a PR once my other one has been merged, but seems this repo is dead.

Windows docker-compose mongo service stops due to "Operation is not permitted" error

Docker: Docker for Windows
OS: Windows 10

As per the docker-compose.yml => mongo service uses the volume ./data mount to store data of mongo service.

For the very first time when you run/build your app, the mongo service + the api service runs fine.

For subsequent restart of the services, the mongo service exits as it faces a issue with windows file system and shows the error as "Operation is not permitted".

Description of the issue that I found: https://stackoverflow.com/a/39013930/8133717

Therefore one can change the ./data mount to docker's internal volume to mitigate the issue caused by data storage in windows file system.

Changes:

version: '3'
services:
  api:
    image: makinhs/rest-api-tutorial
    #...

  mongo:
    image: mongo
    volumes:
      #mounting to docker's own volume system
      - rest-api-tutorial-data:/data/db 
    networks:
      - backend
    ports:
      - "27017:27017"

# adding the volumes needed at the end
volumes: 
  rest-api-tutorial-data:

mongodb

would like to fix these depreciation errors
DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
current Server Discovery and Monitoring engine is deprecated and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.

Typo error in index.js

It seems that you did a massive "search and replace" from LET to CONST ... that made this in line 13 of index.js ๐Ÿ˜„

res.header('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DEconstE');

instead of

res.header('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE');

Wrong ADMIN_PERMISSION in Middleware: auth.permission.middleware.js

Hello and thank you for a wonderful tutorial!

I noticed the wrong admin permission value is used within common\middlewares\auth.permission.middleware.js, line 4:

const ADMIN_PERMISSION = 4096;

Fix #21:

I will create a PR shortly where I have changed that line to reflect the settings already set int the common\config\env.config.js and use this instead:

const ADMIN_PERMISSION = require('../config/env.config')['permissionLevels']['ADMIN'];

if (user_permission_level & ADMIN_PERMISSION) {
   ...
}

That should be a super clean fix, and I hope it helps ๐Ÿ˜„

isset bug in update method

When I want to edit a value, the previous values delete that row entirely and stores only the new value.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.