skip to Main Content
javaScriptcode
app.js
const express = require('express')
const app = express()
const {route} = require('./routes/route')
app.use(express.static('./public'));
app.use(express.json());
app.use(express.urlencoded());
app.use(route)app.listen(5000,()=>{
console.log('server is listening at port 5000...')
})
route.js
const { items } = require("../data");
const express = require("express");
const route = express.Router();
route.get("/", (req, res) => {
  res
    .send('<h1>Home Page</h1><a href="/api/v1/items">products</a>')
    .status(200);
});
route.get("/api/v1/items", (req, res) => {
  const newItems = items.map((item) => {
    const { id, name, img, price, desc, category } = item;
    return { id, name, img, price, desc, category };
  });
  res.json(newItems);
});
route.get("/login.html", (req, res) => {
  res.sendFile(__dirname + "/public/sigup.html");
});

route.post("/", (req, res) => {
  console.log(req.body.emailid);
  if (req.body.emailid === "[email protected]") {
    res.status(200).redirect("signup.html");
    console.log(req.body.emailid);
  } else {
    route.get("/errormsg", (req, res) => {
      const msg = "please Enter valid Email id";
      res.status(401).json({ msg });
    });
  }
});
module.exports = route;

what wrong with my code
TypeError: Cannot read property ‘caseSensitive’ of undefined
at route (D:node_jsmyownapinode_modulesexpresslibrouterindex.js:506:21)
at Layer.handle [as handle_request] (D:node_jsmyownapinode_modulesexpresslibrouterlayer.js:95:5)
at trim_prefix (D:node_jsmyownapinode_modulesexpresslibrouterindex.js:328:13)
at D:node_jsmyownapinode_modulesexpresslibrouterindex.js:286:9
at Function.process_params (D:node_jsmyownapinode_modulesexpresslibrouterindex.js:346:12)
at next (D:node_jsmyownapinode_modulesexpresslibrouterindex.js:280:10)
at urlencodedParser (D:node_jsmyownapinode_modulesbody-parserlibtypesurlencoded.js:91:7)
at Layer.handle [as handle_request] (D:node_jsmyownapinode_modulesexpresslibrouterlayer.js:95:5)
at trim_prefix (D:node_jsmyownapinode_modulesexpresslibrouterindex.js:328:13)
at D:node_jsmyownapinode_modulesexpresslibrouterindex.js:286:9

2

Answers


  1. In app.js try to change

    app.use(express.urlencoded())

    to

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

    This is because this middleware was removed from Express in v4.16.0, and you are expected to use express.urlencoded({ extended: true }) instead.

    Login or Signup to reply.
  2. It is incorrect to use a get method/handler inside post method/handler.

    route.post("/", (req, res) => {
      console.log(req.body.emailid);
      if (req.body.emailid === "[email protected]") {
        res.status(200).redirect("signup.html");
        console.log(req.body.emailid);
      } else {
        route.get("/errormsg", (req, res) => {
          const msg = "please Enter valid Email id";
          res.status(401).json({ msg });
        });
      }
    });
    

    actually errormsg route is actually not needed, so if you don’t want it you can do it as below:

    route.post("/", (req, res) => {
      console.log(req.body.emailid);
      if (req.body.emailid === "[email protected]") {
        res.status(200).redirect("signup.html");
        console.log(req.body.emailid);
      } else {
        const msg = "please Enter valid Email id";
        res.status(401).json({ msg });
      }
    });
    

    or else if you want the errormsg route you should make it a different get method as following:

    route.get("/errormsg", (req, res) => {
      const msg = "please Enter valid Email id";
      res.status(401).json({ msg });
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search