'Configure ExpressJS to use port 3000
I have a app where backend is running on localhost:8000. It should be running on port 3000. For this specific example I am using express server. How can I configure that?
This is my server.js:
const express = require('express');
const cors = require('cors');
const path = require('path');
const testimonialsRoutes = require('./routes/testimonials.routes');
const concertsRoutes = require('./routes/concerts.routes');
const seatsRoutes = require('./routes/seats.routes');
const app = express();
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(express.static(path.join(__dirname + '/client')));
app.use(cors());
app.use('/api/', testimonialsRoutes);
app.use('/api/', concertsRoutes);
app.use('/api/', seatsRoutes);
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/client/NewWaveFest/public/index.html'));
});
app.use((req, res) => {
res.status(404).json({ message: 'Not found' });
});
app.listen(process.env.PORT || 8000, () => {
console.log('Server is running on port: 8000');
});
Solution 1:[1]
app.listen(process.env.PORT || 3000, () => {
console.log('Server is running on port: 3000');
});
Solution 2:[2]
You can use any port by changing the port address || 8000
app.listen(process.env.PORT || 8000(change the value to 3000 or any port number), () => {
console.log('Server is running on port: 8000(change the value to 3000 or any port number)');
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | raizo |
| Solution 2 | Mohiuddin |
