Answer the question
In order to leave comments, you need to log in
Why does express respond with 404 on any post?
I have a devserver on which I write the front-end of the project.
It exists separately from the real server (we will omit the reasons - it is not easy to raise the yiisny server code of the project on the local). To check the functionality of the interface, I request express, or rather static json in the jsondata folder.
GET requests work fine - I get the necessary data in response, but as soon as I replace get with post and express returns 404, although the path remains the same. What is the reason?
It starts according to a task in gulp:
'use strict';
const http = require("http");
const express = require('express');
const reload = require('reload');
const path = require('path');
module.exports = function(gulp, plugins, args, config, taskTarget, server) {
gulp.task('express', function() {
let options = {
dotfiles: 'ignore',
etag: false,
extensions: ['htm', 'html'],
index: false,
maxAge: '1d',
redirect: false,
setHeaders: function (res, path, stat) {
res.set('x-timestamp', Date.now())
}
}
server.use(express.static("../web", options));
server.listen(3000)
});
};
Answer the question
In order to leave comments, you need to log in
Because serve-static, for reasons of common sense, only works with GET and HEAD requests.
A piece of code that is responsible for this was quickly found right in the index:
https://github.com/expressjs/serve-static/blob/mas...
It's
not at all clear why POST should be used with a static verb, it really goes against common sense.
Digging through the express docks, I read that you can track requests through something like a regular expression. server.post('/*')
And dashed off such a thing. It looks like the callback works and responds to post appropriately.
server.post("/*", function(req, res){
res.sendFile(path.join(__dirname, "../../web", req.url))
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question