Javascript 难以设置';邮政';express服务器问题中的路由
我正在尝试验证我的客户端是否可以将信息发布到其服务器。我已在Express服务器中设置了此“路由”Javascript 难以设置';邮政';express服务器问题中的路由,javascript,node.js,express,Javascript,Node.js,Express,我正在尝试验证我的客户端是否可以将信息发布到其服务器。我已在Express服务器中设置了此“路由” // server.js this is the server for the PvdEnroll application. // var express = require("express"), http = require("http"), mongoose = require( "mongoose" ), app = express(); // static
// server.js this is the server for the PvdEnroll application.
//
var express = require("express"),
http = require("http"),
mongoose = require( "mongoose" ),
app = express();
// static file directory for default routing
app.use(express.static(__dirname + "/client"));
app.use(express.urlencoded());
// create Express-powered HTTP server
http.createServer(app).listen(3000);
console.log("Server listening at http://127.0.0.1:3000/");
// set up a post route in the server
app.post("/selections", function (req, res) {
console.log("data has been posted to the server!");
});
app.post("/selections", function (req, res) {
console.log("Some data has been posted to the server from app.js");
})
客户端使用此文件:
var main = function () {
"use strict";
$.getJSON("../data/checkBoxesA.json", function(checkBoxTxt) {
checkBoxTxt.forEach(function (data) {
var $checkbox = "<input type ='checkbox' name = "
+ data.label + "id = 0 UNCHECKED/>";
$(".enroll_actions").append($checkbox);
$(".enroll_actions").append(' ' + data.label + "<br/>");
$(".enroll_actions").append(' ' + data.note + "<br/>");
$(".enroll_actions").append(' '+ "<br/>");
});
});
$(".comment-input").on("click", function (event) {
console.log("Hello World!");
// here we'll do a post to our selections route
$.post("selections", {}, function (response) {
console.log("Client says - We posted and the server responded!");
console.log("Response from server :", response);
console.log("STUBB1");
});
});
console.log("STUBB2");
};
$(document).ready(main);
正在寻找路径,但教程的示例(有效!)在结构上与我的相同(?)使用了一个名称,即“选择”来建立客户端和服务器之间的路由
我在Mac电脑上使用虚拟盒和Vagrant运行服务器
谢谢你的澄清
重新启动服务器时,日志消息现在是“POST 127.0.0.1:3000/html/selections 404(未找到)” 好的,这是一些有用的信息 基本上,您的文件结构或多或少是这样的(有些文件名会有所不同,只需查看一般结构):
- Node.js代码(包括主
文件和.js
)模块.json
:用于存放静态内容的文件夹客户端
:文件夹html
:您当前使用的文件index.html
jQuery.post()
被提供一个相对路径(与绝对路径相对)。这意味着,根据代码所在的位置,它将“猜测”文件的位置
这意味着当您的服务器正在侦听[localhost:port]/selections
时,它试图在[locahost:port]/selections
找到路由!您需要让jQuery post发送到/selections
,而不仅仅是selections
有关更多信息,(相对路径在Javascript/CSS中的作用相同)。有用的代码片段:
以下是有关相对文件路径的所有信息:
- 以“/”开头返回根目录并从那里开始
- 以“./”开头向后移动一个目录并从那里开始
- 以“./../”开头向后移动两个目录并从那里开始(依此类推…)
- 要继续前进,只需从第一个子目录开始并继续前进
您是否尝试通过节点web服务器导航到客户端html而不是本地加载该文件?该静态服务器是否确实允许传递到其他路由?通常,您希望在自定义路由之后像静态服务器一样捕获所有内容。即使静态服务器在找不到文件时确实调用next,也将其放在您的客户之后m路线更安全(更快)由于这种方式,您不会因为文件或目录同名而意外阻止路由。此外,我注意到您的控制台输出显示了一个本地文件路径。如果您将此作为服务器运行,即使它是本地的,也不应该是file://,它应该是http://……发生了什么事?您确定要在本地主机或服务器上访问它吗你只是直接打开本地html文件吗?[编辑:我刚刚注意到@mscdex已经指出了这一点]我在server.js中重新定位了对静态文件目录的引用,这与重新启动服务器没有区别,日志消息现在是“POST 404(未找到)”
POST file:///Users/*******/Projects/r_PE/app/PvdEnroll/client/html/selections net::ERR_FILE_NOT_FOUND