이것은 서버를 만들 때 처음 시도한 것입니다 ...Node.js에서 클라이언트에 응답하는 올바른 방법은 무엇입니까?
미리 정의 된 보안 문자 문자열을 포함하는 연락처 양식 제출을 처리 할 서버를 설정했습니다.
서버, 문의 양식을 수신
보안 문자 문자열은 내가 단순히 보안 문자 문자열이 잘못되면response.end(JSON.stringify(parsedURL));
를 사용하여 구문 분석 문의 양식 쿼리의 JSON으로 응답하려면 다음 예상 한 경우, I 서버가 captcha가 잘못되어 "다시 말해서"클라이언트가 사용자에게 다시 시도하도록 요청하도록 응답하도록합니다. 하지만 어떻게해야할지 모르겠다. 서버에
:
var httpServer = http.createServer(function (request, response)
{
if (/\/contactform\?....../.test(request.url))
{
var parsedURL = url.parse(request.url, true);
var name = parsedURL.query.name;
var email = parsedURL.query.email;
var subject = parsedURL.query.subject;
var enquiry = parsedURL.query.enquiry;
var captcha = parsedURL.query.captcha;
if (captcha !== "testing")
{
// send a "bad" response to the client and include the message "bad captcha"
}
else response.end(JSON.stringify(parsedURL.query));
}
}).listen(8080);
클라이언트는 : 나는 response.end(JSON.stringify(parsedURL));
를 사용하는 경우
$.ajax({
url: "/contactform?..............",
success: function(msg)
{
console.log(msg);
},
error: function(msg)
{
// the "bad captcha" response should be handled here right ?
console.log(msg); // which should be equivalent to console.log("bad captcha");
}
});
클라이언트 (jQuery를)는 "성공"고 생각한다.
클라이언트에서의 ajax 요청의 "오류"부분이 실행되도록 서버에서 어떻게 응답해야합니까?
서버가 전혀 응답하지 않는 경우, 즉 예외, 실제 오류와 같은 무언가 잘못된 서버 측이 발생했을 때만 "오류"부분을 처리해야합니다. 내 평가 서버에 예상 된 결과가 없어?
내가 대신 같이 두 경우 모두 response.end(...);
을 사용해야 : 클라이언트에서
var httpServer = http.createServer(function (request, response)
{
if (/\/contactform\?....../.test(request.url))
{
var parsedURL = url.parse(request.url, true);
var name = parsedURL.query.name;
var email = parsedURL.query.email;
var subject = parsedURL.query.subject;
var enquiry = parsedURL.query.enquiry;
var captcha = parsedURL.query.captcha;
var response = JSON.stringify(parsedURL.query);
if (captcha !== "testing") response = "bad captcha";
response.end(response);
}
}).listen(8080);
: 서버에
즉
$.ajax({
url: "/contactform?..............",
success: function(msg)
{
console.log(msg); // msg will either be the stringified object or "bad captcha"..
}
});
때 요청은 서버에서 성공적으로 수신되었지만 서버는 파일을 받기를 원합니다. 서버로부터의 응답이 "오류"(즉, 오류)로 보내지는 경우, 클라이언트는 무언가가 누락되었거나 무엇이든지간에 알 수 있습니다. 클라이언트의 Ajax 코드에서 "오류"블록에 의해 처리됨) 또는 실제로 발생한 결과를 나타내는 적절한 메시지와 함께 "성공"으로 처리 할 수 있습니까?
이 질문에 대한 답변을 여기에 있습니다 : http://stackoverflow.com/questions/14154337/how-to-send-a-custom-http-status-message-in-node-express – xShirase
나는 그것을 보았지만 나는 익스프레스를 사용하지 않고 Node.js를 먼저 배우고 내 길을 빌드하려고합니다. –