1 . give new folder name ServerSide or backendServer
- write this text $ npm init -y
- create file Server.js
- install this npm service $ npm install express mysql cors cookie-parser bcrypt jsonwebtoken multer path nodemon 5.create 5 impotent folder in MVC Public/images , Routers , Models , controllers , Utils ,
- package.json add some text "main": "Server.js", "type": "module", "scripts": { "start": "nodemon Server.js" }
- Utils/ db.js file create and connect mysqul or other database
import mysql from 'mysql'
const db = mysql.createConnection({
host: 'your_mysql_host',
user: 'your_mysql_user',
password: 'your_mysql_password',
database: 'your_mysql_database'
})
db.connect(function(err) {
iif (err) {
console.error('Error connecting to MySQL: ' + err.stack);
return;
}
console.log('Connected to MySQL as id ' + db.threadId);
})
export default db;
8.server.js work
import express from "express";
import cors from 'cors'
const app = express()
const port = 5175 ;
const localhostPort = 7175 ;
app.use(cors({
origin: [http://localhost:${localhostPort }
],
methods: ['GET', 'POST', 'PUT', "DELETE"],
credentials: true
}))
app.use(express.json())
app.listen(port , () => {
console.log("Server is running")
})
app.use(cors(db));
- Routers file all Route here contoll.
- controllers fill ey all logic controll here .