0

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');
});
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
present_perfect
  • 149
  • 1
  • 2
  • 10
  • `app.listen(process.env.PORT || 3000, ..)` instead of 8000 – Yevhen Horbunkov Jul 07 '20 at 14:46
  • 1
    This answer could help you https://stackoverflow.com/questions/40714583/how-to-specify-a-port-to-run-a-create-react-app-based-project – vitomadio Jul 07 '20 at 14:50
  • You could do what @YevgenGorbunkov suggested. But the problem is tho once you hit ```npm start``` you will see something in the terminal saying like 'port 3000 is in use, do you want to open a different port'. – TechSelfLearner Jul 07 '20 at 14:51
  • thank you, but I need 8000 too. Can it be configured for both ports somehow, one for backend one for frontend? – present_perfect Jul 07 '20 at 14:55

2 Answers2

2
app.listen(process.env.PORT || 3000, () => {
console.log('Server is running on port: 3000');
});
raizo
  • 144
  • 2
  • 7
1

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)');
});
Mohiuddin
  • 80
  • 5