Home > Mobile >  Not Able to hit Node/Express Backend
Not Able to hit Node/Express Backend

Time:07-25

I have created a backend service using Node and Express. I am not able to hit any route for this application. If routes are misconfigured then I should get an error on console, but I am not getting an error as well.

index.js

const http = require('http');
const app = require('./app');
const server = http.createServer(app);

const {API_PORT} = process.env;
const port = process.env.PORT || API_PORT;


server.listen(port,()=>{
    console.log(`Server Running on port ${port}`);
})

app.js

require("dotenv").config();

const express = require("express");
const loginRoute = require("./routes/login");
const app = express();
app.use(express.json());

app.use('/unsecure',loginRoute);

module.exports = app;

login.js (Route)

const express = require("express");
const router = express.Router();
const loginController = require("../controller/login.controller");


router.post('/login',loginController.loginUser);
router.post('/register',loginController.registerUser);

module.exports = router;

.env

API_PORT=4001
DB_HOST= hostname
DB_PASSWORD= password
DB_PORT=5432
DB_USERNAME= username

CodePudding user response:

reminder that when you added '/unsecure' you also need to use it during post request

app.use('/unsecure',loginRoute);
//request from client:
'/unsecure/login' // must be of type POST
'/unsecure/register' // must be of type POST

CodePudding user response:

I was exploring your code, you have not given the controller code, but still i found no error, all url were working. I think you might be hitting wrong url

your url should be -

{root}/unsecure/login
{root}/unsecure/register

According to .env you have given it should be -

http://localhost:4001/unsecure/login    
http://localhost:4001/unsecure/register
  • Related