I'm trying to set up a simple REST API with express.js. Having trouble implementing a delete function from client-side.
Routes:
module.exports = function(server) {
server.get('/entries/:id', entries.get) // working
server.del('/entries/:id', entries.destroy)// not working
}
i have a dummy delete button set up like this (using handlebars.js):
<form id='deleteform' action={{path}} method='POST'>
<input type='hidden' name='_method' value='DEL' />
<input type='hidden' name='_csrf' value={{csrf_token}} />
<button class='submit' type='submit' name='DELETE'>
</form>
when i click the button, i get "Cannot POST /entries/1386113642" or whatever other url i set {{path}} to - however, GET requests for the same url work fine. i also have methodOverride() set in my express config file. what am i doing wrong here?
edit: here is my entire app.configure() call:
app.configure(function() {
app.use(function(req, res, next) {
res.locals.pkg = pkg
next()
})
app.use(express.favicon())
var rootPath = path.normalize(__dirname + '/..')
var basePath = path.normalize(__dirname + '/../..')
app.use(express.static(rootPath + '/public'))
app.use(express.static(basePath + '/backbone/'))
app.set('views', __dirname + '/../../static')
app.engine('hbs', exphbs({
defaultLayout: 'main',
extname: '.hbs',
partialsDir: '../static',
layoutsDir: '../static/layouts'
}))
app.set('view engine', '.hbs')
app.use(express.cookieParser())
app.use(express.bodyParser())
app.use(express.methodOverride())
app.use(express.session({
secret: 'aekugwieufgkdjfhaisuefhwijfeijf',
store: new RedisStore({host:'localhost', port: 6379, client: redis })
}))
app.use(function(req, res, next) {
res.locals.csrf_token = req.session._csrf
next()
})
if (process.env.NODE_ENV !== 'test') {
app.use(express.csrf())
}
app.use(app.router)
})
}
DEL
应该为DELETE
:
<input type='hidden' name='_method' value='DELETE' />
i also have methodOverride() set in my express config file
Post this code. It's not a config file. It is code that is highly sensitive to the order of middleware functions. In particular, make sure bodyParser
comes before methodOverride
which comes before app.router
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.