Git Product home page Git Product logo

nodejs-api-auth-backend's Introduction

nodejs-api-auth-backend

Used tools

Developed under:

  • Node.js 16.13.0
  • NPM 8.1.0
  • MongoDB 5.0.2

One of the first step when developing a Node.js project, especially a REST API server, is to be able to accept requests from clients and authenticate the users whether to only give access to whom granted or to record the use. Apart from usual actions (sign up/in/out), it is possible to protect routes (see profile routes) with passport

Features

  • Data stored in MongoDB
  • Sign up with a name and a password
  • Sign in with a name and a password
  • Sign out
  • Protected routes (Profile) through JWT

How to run

  1. Clone the repository:

git clone https://github.com/mphuget/nodejs-api-auth-backend.git

  1. Go to the repository:

cd nodejs-api-auth-backend

  1. Install the dependencies (removed from Git repository):

npm install

  1. Create a directory for MongoDB data (select where to store them)

mkdir ./data

  1. Run the MongoDB server from another directory than the data one (in a first terminal)

mongod --dbpath ./data

  1. Rename the .env-example file into .env

  2. Change the content of the .env file. The critical data are port on which the server is running, and the MongoDB URL

  3. On another terminal, run the server

node server.js

If everything went smoothly, you should see a running on port message and connected to database messages

The different routes were tested under Postman

How to reuse

From this code, it is possible to add an API

  1. Create a file in the routes folder where you will provide all the routes for your API

  2. Create a file in the controller folder where the corresponding behaviour will be added

  3. (If needed) Add a file in the models folder for the model

  4. Modify the server.js file to add the routes

Et voilà! Enjoy!

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.