-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
80 lines (46 loc) · 2.22 KB
/
Copy pathserver.js
File metadata and controls
80 lines (46 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import express from 'express' // avoid require
import dotenv from 'dotenv' // use type:"module" in package.json
import authRouter from './routes/auth.route.js'
import messageRouter from './routes/messageRouter.js'
import userRouter from './routes/users.router.js'
import morgan from 'morgan';
import { connectToMongodb } from './db/connectTomongodb.js';
import { app, server } from './socket/socket.js'
import cors from "cors";
app.use(express.json());
app.use(morgan("dev")); //for diplaying request
dotenv.config(); // for env setup files
app.use(cors())
const PORT = process.env.PORT ;
app.use( "/api/auth" , authRouter ) ; // authentication
app.use( "/api/messages" , messageRouter ) ; // mantain conversation
app.use( "/api/users" , userRouter ) ; // get users except me
server.listen(PORT , ()=>{
connectToMongodb();
console.log(`server running in : http://localhost:${PORT}`);
})
{/*
for frontend vite gives bundler
// npm install express mongoose dotenv jsonwebtoken cookie-parser bcryptjs
for backend create the structure
add a variable in script to run
"server":"nodemon backend/server.js"
"start":"node backend/server.js"
npm run server
nodemon:when u update file u need to reRUN the server .. for production only
so add npm install nodemon --save-dev
1) setup env file npm install dotenv require('dotenv') dotenv.config(;)
2) structure with router .. controller
3) introduce global catches for standard error handling in try cathes for custom error { next(new error("mssg")) } ..catch(error){ next(error) }
4) setup mongodb
1. create a new project in mongo account copy password .. get connection string.
1.1 in connction string add /table as well
2. connect our backend to the mongo projects connection string by imort mongoose ... mongoose.connect(string);
5) export await mongoose.Schema ... mongoose.model
*/}
{/*
?? D E P L O Y B A C K E N D
upload to git without { E N V } file and N O D E M O D U L E S add in .gitignore file .env /node_modules
// node modules will downloaded seing to packegae file
// so add in { B U I L D } Script in package npm insatll ... npm run build
*/}