Discord Clone – Learn MERN Stack with WebRTC and SocketIO

01 - Introduction

02 - Preparing server and authentication possibility

001 Introduction

002 Node.js installation

2021 14~16

003 Creating project structure and initializing a Project

npm init -y

004 Installing necessary packages with NPM

pnpm i --save express express-joi-validation joi jsonwebtoken mongoose cors dotenv bcryptjs
pnpm i --save nodemon

005 Creating Express Server

// discord-backend/server.js
const express = require("express");
const http = require('http');
const cors = require('cors');
const mongoose = require('mongoose');
require('dotenv').config()

const PORT = process.env.PORT || process.env.API_PORT

const app = express()
app.use(express.json())
app.use(cors())

const server = http.createServer(app)

server.listen(PORT, () => {
    console.log(`Server is running on ${PORT}`)
})

006 Nodemon - adding script to automatically restart server after changes

{
  "scripts": {
    "start": "nodemon server.js"
  }
}

007 Creating MongoDB Database in Cloud

mongo cloud

009 Connecting with database from our server and testing connection

// discord-backend/server.js
/// 

const server = http.createServer(app)

// 注意mongodb和mongoose版本对应
mongoose.connect(process.env.MONGO_URI, {
    useNewUrlParser: true, useUnifiedTopology: true
})
    .then(() => {
        server.listen(PORT, () => {
            console.log(`Server is running on ${PORT}`)
        })
    })
    .catch((err) => {
        console.log('database connection failed. Server not started')
        console.error(err)
    })

010 Creating folder structure and authentication routes

// discord-backend/routes/authRoutes.js
const express = require('express');
const router = express.Router()

router.post('/register', (req, res) => {
    res.send('register route')
})

router.post('/login', (req, res) => {
    res.send('login route')
})

module.exports = router
// discord-backend/server.js
/// 

const authRoutes = require('./routes/authRoutes');

const PORT = process.env.PORT || process.env.API_PORT

const app = express()
app.use(express.json())
app.use(cors())

// register the routes
app.use('/api/auth', authRoutes)

const server = http.createServer(app)

// ...

011 Testing routes with Postman

012 Moving handlers to controllers folder

// discord-backend/controllers/authController.js
const postLogin = require('./postLogin')
const postRegister = require('./postRegister')

exports.controllers = {
    postLogin, postRegister
}

// discord-backend/controllers/postLogin.js
const postLogin = async (req, res) => {
    res.send('login')
}

module.exports = postLogin

// discord-backend/controllers/postRegister.js
const postRegister = async (req, res) => {
    res.send('register')
}

module.exports = postRegister
// discord-backend/routes/authRoutes.js
const express = require('express');
const authControllers = require('../controllers/authController')
const router = express.Router()

router.post('/register', authControllers.controllers.postRegister)

router.post('/login', authControllers.controllers.postLogin)

module.exports = router

013 Adding request validation with Joi


  目录