Express App GET路由在生产环境中不起作用

 收藏

我在本地主机(DEV)和NameCheap(PROD)上运行Node JS服务器。

两种环境都运行服务器。两者都托管Web应用程序。我遇到的问题是尝试在Javascript中使用FETCH API时。

server.js

var express = require("express");
var path = require("path");
var db = require("./db");
var app = express();
const port = 3000;

app.set("port", process.env.PORT || port);

app.route("/queryDB").get(function(req, res) {
  console.log(req.query.query);

  db.query(req.query.query, function(err, rows, fields) {
    if (err) res.send(err);
    else res.send(rows);
  });
});

app.use(express.static(__dirname, { extensions: ["html"] }));
app.use("/css", express.static(path.join(__dirname + "/css")));
app.use("/js", express.static(path.join(__dirname + "/js")));
app.use("/images", express.static(path.join(__dirname + "/images")));
app.use("/icons", express.static(path.join(__dirname + "/icons")));

app.listen(app.get("port"), "0.0.0.0", () =>
  console.log("Started listening on %s", app.get("port"))
);

使用提取功能

export async function queryDB(query) {
  const res = await fetch("../queryDB?query=" + query);
  try {
    if (res.ok) {
      return res.json();
    } else {
      // eslint-disable-next-line no-console
      console.log(res);
    }
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
  }
}

I originally had it set up with app.route and only using app.get. After research started using route.

Tried using path.join(__dirname + "/queryDB")

The fetch should return query results from the MySql DB that is set up. Currently I am receiving http://url.com/queryDB?query=SELECT%20Name,%20DATE_FORMAT(Date,%20%22%25W%2C%20%25M%20%25e%2C%20%25Y%22)%20as%20Date,%20Time_Format(Time,%22%25l%3A%25i%20%25p%22)%20as%20Time,%20Description%20FROM%20event%20WHERE%20IsRSVP%20=%201%20ORDER%20BY%20Date,%20Time%20ASC

with the error Failed to load resource: the server responded with a status of 404 (Not Found)

在我的DEV环境中,返回数组中的查询结果。

EDIT: When Running DEBUG=express:* node server from the name cheap console. I see the routes were created

  express:router:route new '/queryDB' +1ms
  express:router:layer new '/queryDB' +0ms
  express:router:route get '/queryDB' +0ms

编辑:

更新了Server.JS

var express = require("express");
var path = require("path");
var app = express();
const port = 3000;

app.set("port", process.env.PORT || port);

app.route("http://url.com/query").all(function(req, res) {
  console.log("Query");
  res.send("Query Res");
  return;
});

app.use(express.static(__dirname, { extensions: ["html"] }));
app.use("/css", express.static(path.join(__dirname + "/css")));
app.use("/js", express.static(path.join(__dirname + "/js")));
app.use("/images", express.static(path.join(__dirname + "/images")));
app.use("/icons", express.static(path.join(__dirname + "/icons")));

app.listen(app.get("port"), () =>
  console.log("Started listening ons %s", app.get("port"))
);

更新功能

export async function queryDB(query) {
  const res = await fetch("http://url.com/query");

  try {
    if (res.ok) {
      console.log(res);
      return res;
    } else {
      // eslint-disable-next-line no-console
      console.log(res);
    }
  } catch (err) {
    // eslint-disable-next-line no-console
    console.log(err);
  }
}

回复
  • 裝B控 回复

    Judging from the comments the fix should be to replace the route you are posting to in your code from app.route.get to app.route.post if you are using that route to exclusively post or app.route.all if you wish both POST and GET functionality.