Express middleware not working => app.use

Question:
The express middleware “app.use” function stopped working suddenly. Now when I call the URL the function doesn’t work as expected
Repl link:

const express = require('express');
const app = express();
const cors = require('cors');
const port = 3000;

app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.get( '/', (req, res) => {
  res.send('Root has been reached!');
  console.log('Root has been reached');
});

app.post('/usermsg', (req, res) => {
  const body = req.body;
  console.log(body);
  res.send('http request from postman received');
});

const middleware = require('./subscription');

app.use('/usermsg', middleware.router);

app.listen(port, () => {
  console.log(`App listening at :${port}`);
});

Hey @lc96 welcome to the forums!

Are you receiving any specific error(s)?

A potential issue could be you having a POST route and a middleware for /usermsg, which could possibly clash. If the middleware is supposed to handle POST requests to /usermsg , it should be added after the POST route definition. Make sure that the middleware is actually sending something that can be used by app.use(). Here is the fixed code →

const express = require('express');
const app = express();
const cors = require('cors');
const port =  3000;

app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.get('/', (req, res) => {
  res.send('Root has been reached!');
  console.log('Root has been reached');
});

app.post('/usermsg', (req, res) => {
  const body = req.body;
  console.log(body);
  res.send('HTTP request from Postman received');
});

const middleware = require('./subscription');

// Use middleware after the route definition
app.use('/usermsg', middleware.router);

app.listen(port, () => {
  console.log(`App listening at ${port}`);
});

Hi,

Thanks for the prompt response. However, the issue doesn’t seem to be that. It works fine when I deploy the program and use the public url. However, doesn’t work with the webview url, which makes it super difficult to debug the code as the errors only occur in the ‘log’ after deploying the program