Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
POST request does not get handled in express

I use ejs, express, nodeJS and mySQL. This code gives me an error : Cannot POST /search . I think the idex.ejs and the app.js is fine but I messed up the searchRouter ...

app.js

const express = require('express')
const app = express()

app.use('/static',express.static('static'))
app.set('view engine','ejs')

const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())

const index = require('./router/indexRouter')
const upload = require('./router/uploadRouter')
const detail = require('./router/detailRouter')
const edited = require('./router/editedRouter')
const search = require('./router/searchRouter')

app.use("/",index)
app.use(upload)
app.use(detail)
app.use(edited)
app.use(search)

const port = 8080

app.listen(port, () => console.log(`Server up and running on port: ${port} !`))

searchRouter

const express = require('express')
const router = express.Router()
const dbCon = require('../database.js')

router.get('/search',(req,res) =>{
    const searched = req.body.search
    const query = `SELECT * FROM memes WHERE (title like "${searched} OR user like "${searched})"`

    dbCon.query(query, (err, results) =>{
        if(err) {
            console.log(err)
        }
        console.log(results)
        res.render('detail',{memes:results})
    })
})

module.exports = router
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you want to handle a POST request to your /search route, you'll have to register it correctly using express.

Like this:

router.post('/search', (req, res) => {
  /* ... */
});

Please note - in the code you are showing you only registered for a GET request on said route. However (!) just from a logical standpoint, I think a GET request for a /search route makes more sense anyway.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!