Process to make node server:
- run code npm init – y
- run code npm i express mysql nodemon
- After install no-2 then create page index.js
- Put code in index.js
import express from “express”
import express from “express”;
import mysql from “mysql”;
const app = express()
const db = mysql.createConnection({
host: “localhost”,
user: “root”,
password: “”,
database: “nodedb”
})
app.get(“/”, (req, res) => {
res.json(“Hello this is backend”)
})
app.get(“/test”, (req, res) => {
const q = “SELECT * FROM `test`”
db.query(q, (err, data) => {
if (err) return res.json(err)
return res.json(data)
})
})
app.listen(8800, () => {
console.log(“connect DB”)
}) - run code npm start if issue then modify package.json after this “main”: “index.js”,
“type”: “module”,
And add line in scripts
“start”: “nodemon index.js” - then again run npm start
- open run mysql then same db connection needed.
This is a way to start first to make node server with mysql connecttion