DEV Community

swapnil ahmed shishir
swapnil ahmed shishir

Posted on

Node Server Setup

1 . give new folder name ServerSide or backendServer

  1. write this text $ npm init -y
  2. create file Server.js
  3. 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 ,
  4. package.json add some text "main": "Server.js", "type": "module", "scripts": { "start": "nodemon Server.js" }
  5. 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));

  1. Routers file all Route here contoll.
  2. controllers fill ey all logic controll here .

Top comments (0)