├── index.js ├── .gitignore ├── todo.txt ├── extra ├── notfound.js └── errhandler.js ├── db └── connectdb.js ├── routes └── routes.js ├── package.json ├── schemas └── model.js ├── README.md ├── app.js └── controllers └── controllers.js /index.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .env 3 | -------------------------------------------------------------------------------- /todo.txt: -------------------------------------------------------------------------------- 1 | limit cors ip's acces 2 | limit db ip's acces 3 | -------------------------------------------------------------------------------- /extra/notfound.js: -------------------------------------------------------------------------------- 1 | const NotFound = (req,res) => { 2 | res.status(404).send('Not Found 404') 3 | } 4 | 5 | module.exports = NotFound -------------------------------------------------------------------------------- /extra/errhandler.js: -------------------------------------------------------------------------------- 1 | const errhandler = (err,req,res,next) => { 2 | res.status(500).send('Server Breakdown') 3 | } // create a middleware function to handle errors 4 | module.exports = errhandler // export the middleware function -------------------------------------------------------------------------------- /db/connectdb.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); // import mongoose 2 | 3 | const connectDB = (url) => { 4 | return mongoose.connect(url, { 5 | useNewUrlParser: true, 6 | useUnifiedTopology: true 7 | }); 8 | }; // create a function to connect to the database 9 | 10 | module.exports = connectDB; // export the connectDB function 11 | -------------------------------------------------------------------------------- /routes/routes.js: -------------------------------------------------------------------------------- 1 | const express = require('express') // import express 2 | let router = express.Router() // create a router object 3 | const {addParticipant,getParticipants} = require('../controllers/controllers') // import the controllers 4 | 5 | router.post('/addp',addParticipant) // add a participant 6 | router.get('/getp',getParticipants) // get all participants * 7 | 8 | module.exports = router // export the router object -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "express_backend_template", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "app.js", 6 | "scripts": { 7 | "start": "nodemon app.js" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "cors": "^2.8.5", 14 | "dotenv": "^16.3.1", 15 | "express": "^4.18.2", 16 | "express-rate-limit": "^7.1.5", 17 | "helmet": "^7.1.0", 18 | "jsonwebtoken": "^9.0.1", 19 | "mongoose": "^7.4.1", 20 | "nodemon": "^3.0.1", 21 | "xss-clean": "^0.1.4" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /schemas/model.js: -------------------------------------------------------------------------------- 1 | const {Schema , model} = require('mongoose'); 2 | 3 | const userSchema = new Schema({ 4 | name : { 5 | type : String, 6 | trim : true, 7 | }, 8 | email : { 9 | type : String, 10 | unique : true, 11 | }, 12 | team : { 13 | type : String, 14 | }, 15 | phoneNumber : { 16 | type : String, 17 | }, 18 | institution : { 19 | type : String, 20 | //enum : ['ESI','ESTIN','USTHB','ESI SBA','Other'], 21 | }, 22 | placeToStay : { 23 | type : Boolean 24 | }, 25 | anythingToAdd : { 26 | type : String, 27 | }, 28 | }) 29 | 30 | module.exports = model('User',userSchema); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## step 1 : run this sample command to clone this template to your pc 2 | `git clone https://github.com/n3xusss/backend_express_template` 3 | ## step 2 : after cloning the repo run a terminal inside the cloned repo folder and run this command to install all dependencies 4 | `npm install` 5 | ## step 3 : after install all dependencies you will need to run the server next on your terminal just type this sample command : 6 | `npm start` 7 | ## Template Structure: 8 | 9 | - controllers 10 | - controllers.js 11 | - db 12 | - connectdb.js 13 | - extra 14 | - errhandler.js 15 | - notfound.js 16 | - routes 17 | - routes.js 18 | - schemas 19 | - model.js 20 | - app.js 21 | - package.json 22 | ## to add new participant 23 | `/api/v1/addp - POST` 24 | ## to get all participants originzed by team 25 | `api/v1/getp - GET ` 26 |
27 | dont forget to include your mongodb cluster in .env file after the name "MONGO = your_cluster_key" 28 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | //importin all requirements 2 | const express = require('express') 3 | const app = express() 4 | const connectdb = require('./db/connectdb') //db connection fonction 5 | const routes = require('./routes/routes') //products routes 6 | const NotFound = require('./extra/notfound') //not found handler 7 | const errhandler = require('./extra/errhandler') //err handler 8 | const cors = require('cors') // limit cors latter -_- 9 | const helmet = require('helmet') // secure the app -_- 10 | const xss = require('xss-clean') // secure the app -_- 11 | const rateLimit = require('express-rate-limit') // secure the app -_- 12 | require('dotenv').config() 13 | 14 | // midddelwares 15 | app.use(cors()) //handle cors 16 | app.use(express.json()) 17 | app.use(helmet()) //handle helmet 18 | app.use(xss()) //handle xss 19 | app.use(rateLimit({ //handle rate limit 20 | windowMs : 10 * 60 * 1000 , // 10 mins 21 | max : 100 // limit each IP to 100 requests per windowMs 22 | })) 23 | app.use('/api/v1/',routes) // link the products routes 24 | 25 | 26 | 27 | //handelers 28 | app.use(NotFound) //handle wrong route pathes 29 | app.use(errhandler) //handle server errs 30 | //start the server 31 | 32 | const port = process.env.PORT || 8088 33 | const start = async () => { 34 | try { 35 | await connectdb(process.env.MONGO) // connect to db 36 | app.listen(port , console.log('listenin on port ' + port )) 37 | 38 | }catch(err) { 39 | console.log(err) 40 | } 41 | } 42 | start() -------------------------------------------------------------------------------- /controllers/controllers.js: -------------------------------------------------------------------------------- 1 | const User = require('../schemas/model.js'); 2 | 3 | const isEmailValid = (email)=> { 4 | const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; 5 | return emailRegex.test(email); 6 | }; 7 | 8 | const checkEmailexist = async (email) => { 9 | const exist = await User.find({ email }); 10 | if (exist.length > 0) { 11 | return true; 12 | } 13 | return false; 14 | 15 | }; 16 | 17 | const TeamFull = async (team) => { 18 | const exist = await User.find({ team }); 19 | if (exist.length >= 5) { 20 | return true; 21 | } 22 | return false; 23 | 24 | 25 | } 26 | 27 | const addParticipant = async(req,res) =>{ 28 | try{ 29 | const {name,email,team,phoneNumber,institution,placeToStay,anythingToAdd} = req.body; 30 | if(!name || !email || !team || !phoneNumber || !institution || !placeToStay || !anythingToAdd){ 31 | return res.status(400).json({msg : "Please enter all fields"}); 32 | } 33 | if(!isEmailValid(email)){ 34 | return res.status(400).json({msg : "Please enter a valid email"}); 35 | } 36 | if(await checkEmailexist(email)){ 37 | return res.status(400).json({msg : "This email is already registered"}); 38 | } 39 | if(await TeamFull(team)){ 40 | return res.status(400).json({msg : "This team is full"}); 41 | } 42 | const info = { 43 | name, 44 | email, 45 | team, 46 | phoneNumber, 47 | institution, 48 | placeToStay, 49 | anythingToAdd 50 | 51 | } 52 | const user = await User.create(info) 53 | if(user) { 54 | return res.status(200).json({msg : "Participant added successfully"}); 55 | } 56 | return res.status(500).json({msg : "Something went wrong"}); 57 | }catch(err){ 58 | return res.status(500).json({msg : err.message}); 59 | } 60 | } 61 | 62 | const getParticipants = async(req,res) =>{ 63 | try{ 64 | let users = User.find({}).sort('team') 65 | users = await users; 66 | if(users){ 67 | return res.status(200).json({users}); 68 | } 69 | return res.status(500).json({msg : "Something went wrong"}); 70 | }catch(err){ 71 | return res.status(500).json({msg : err.message}); 72 | } 73 | } 74 | module.exports = { 75 | addParticipant, 76 | getParticipants 77 | } --------------------------------------------------------------------------------