im not using port 4000 it said im using it i tried to kill any process using 4000 port i didnt find any here the error msg : enter image description here
i tried to run the backend project and here’s the index.js code :
const express = require('express');
const sql = require('mssql');
const cors = require('cors');
const app = express();
const port = 4000;
// SQL Server configuration
const config = {
user: 'machine datas',
password: 'das',
server: 'MSI',
database: 'Mach',
dialect: "mssql",
options: {
encrypt: true,
trustServerCertificate: true , // Change to true for self-signed certificates
trustedConnection : false ,
enableArithAbort : true ,
instancename : 'SQL EXPRESS' ,
}
};
// Connect to SQL Server
sql.connect(config)
.then(pool => {
if (pool.connecting) {
console.log('Connecting to SQL Server...');
} else {
console.log('Connected to SQL Server!');
}
})
.catch(err => {
console.error('SQL Server connection error:', err);
});
// Route for the root path
app.get('/', (req, res) => {
res.send('Welcome to the API!');
});
// route
app.get('/data', async (req, res) => {
try {
const result = await sql.query('SELECT * FROM aaxeee');
console.log('Data fetched from SQL Server:', result.recordset);
res.json(result.recordset);
} catch (err) {
console.error('SQL query error:', err);
res.status(500).send('Server error');
}
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
app.use(cors()); // This will allow all origins by default
app.get('/api/items', (req, res) => {
res.json([{ /* */ }]);
});
app.listen(4000, () => console.log('Server running on port 4000'));
i didn’t understand fully what is happening even tough i changed the port multiple times and killed any process used by any port i tried to enter i don’t know if it could be the configuration or something else its my first time creating a backend project .
3
Answers
Sometimes restarting the pc can solve this type of issues, you should also check all the running services and their port and try to stop them manually.
You are listening on port 4000 twice, you need to remove one of them:
app.listen(port, () => {})
orapp.listen(4000, () => {})
You need to kill your server.you need to run this command
sudo kill -9 $(sudo lsof -t -i:4000)