简体   繁体   English

Express POST在Apache上返回404

[英]Express POST returns 404 on Apache

There is a React app that has express requesting api/login information from Mongodb and checking the password input against it, otherwise it doesn't allow you to access the website. 有一个React应用程序可以向Mongodb明确请求api / login信息,并对照它检查密码输入,否则,您将无法访问该网站。

Locally everything works great. 在本地,一切正常。 When we moved all the build files to the apache server the console returns POST https://websitepath.com/api/login 404 (Not Found) 当我们将所有构建文件移至apache服务器时,控制台将返回POST https://websitepath.com/api/login 404(未找到)

在此处输入图片说明

Any idea of what could be a problem and why it works locally but doesn't work on apache? 关于什么可能是问题以及为什么它在本地有效但在apache上无效的任何想法? Node is installed and Express is running there successfully on port 4000. 已安装节点,Express已在端口4000上成功运行。

Here is the code for index.js 这是index.js的代码

var express = require('express');
var bodyParser= require('body-parser')
var MongoClient = require('mongodb').MongoClient
var sha1 = require('sha1');
var db;

const PORT = 4000;
var app = express();

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


app.use('/api/login', function (req, res) {

  if (!req.body.password) return res.status(400).send('bad_request!')

  db.collection('user').find().toArray(function(err, results) {
    if (err) return res.status(500).send('something_wrong!');

    var checker = false;

    results.forEach(function (entry) {
      if (entry.password === sha1(req.body.password)) checker = true;
    })

    if (checker) {
      res.send('success')
    } else {
      return res.status(403).send('Unfortunately password is incorrect. Please try again.');
    }
  })
})

MongoClient.connect('mongodb://localhost:27017/test', (err, database) => {
  if (err) return console.log(err)
  db = database
  app.listen(PORT, function() {
    console.log('Express server is up on port ' + PORT);
  });
})

Here is the code for the AuthService.js 这是AuthService.js的代码

 import axios from 'axios';
import qs from 'qs';

const AuthService = {
  isLogged: false,
  login(data, cb) {

    axios.post('/api/login',  qs.stringify(data)).then(
      (res) => {
        this.isLogged = true;
        cb(null, res);
      }
    ).catch((error) => {
      console.error('error occured', error);
      cb(error.response.data);
    })
  },
}

export default AuthService;

Your question doesn't mention proxying the node.js application, so I'm guessing that's where the problem is - specifically, the node application is not being proxied. 您的问题没有提到代理node.js应用程序,因此我猜这就是问题所在-特别是,没有代理该节点应用程序。

In short, what you appear to be trying to do is something like this: 简而言之,您似乎想做的事情是这样的:

Apache is listening on port 443 (the HTTPS port) and serving web pages at various paths (presumably, everything except paths starting with /api ). Apache正在监听端口443(HTTPS端口),并在各种路径(大概是除以/api开头的路径之外的所有路径)上提供网页。

You want the web server to also serve the paths used by your node.js API (eg. /api/login and others) on port 443. 您希望Web服务器还在端口443上提供您的node.js API使用的路径(例如/api/login等)。

But two distinct applications (Apache and your node.js app) cannot both listen on port 443 - Apache is binding it and serving its own pages. 但是,两个不同的应用程序(Apache和您的node.js应用程序)无法同时监听端口443-Apache对其进行绑定并提供自己的页面。 If you try to change the port on your node.js application, it will fail to start and give you an error indicating that port 443 is already bound by another application. 如果尝试更改node.js应用程序上的端口,它将无法启动,并显示错误消息,表明端口443已被另一个应用程序绑定。

There is a simple test for this: navigate to http://websitepath.com:4000/api/login . 为此有一个简单的测试:导航至http://websitepath.com:4000/api/login If you can see your API login page (ie. the node.js application is listening on port 4000), that means the problem is NOT with your node application, it's with Apache's proxy configuration. 如果您可以看到您的API登录页面(即node.js应用程序正在侦听端口4000上),则意味着问题不在于您的节点应用程序,而是Apache的代理配置。

The solution to this is setting up Apache as a proxy . 解决方案是将Apache设置为代理 This will allow Apache to serve its own pages and forward the request to another service based on the path. 这将允许Apache服务于其自己的页面,并根据路径将请求转发至另一服务。 So you could certainly set it up so paths that start with /api/... are forwarded to http://localhost:4000/api/... and any other paths are served by Apache directly. 因此,您当然可以进行设置,以便将以/api/...开头的路径转发到http://localhost:4000/api/...而其他任何路径都由Apache直接提供。

Setting up a proxy is not terribly difficult, but it depends a lot on your specific circumstances, so I'm not going to attempt to explain all the ins & outs. 设置代理并不十分困难,但这在很大程度上取决于您的具体情况,因此,我不会尝试解释所有的来龙去脉。 I'd recommend starting with the mod_proxy documentation. 我建议从mod_proxy文档开始。 There are also about a million tutorials out there; 还有大约一百万个教程。 Digital Ocean's documentation is good - I've used it in the past. Digital Ocean的文档很好-我过去曾经使用过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM