Javascript 一个node.js服务器文件是否可以用于服务HTML页面和处理来自该页面的POST请求?
我对运行Web服务器及其体系结构是完全陌生的。我目前正在构建一个web应用程序,它有一个基于HTML的GUI,使用一些JavaScript部分处理用户数据,然后将其作为POST请求发送到web服务器 我的问题很简单:同一个node.js服务器是否可以用于处理POST请求,或者是否需要两个不同的“服务器”(即两个不同的侦听器和端口) 如果是这样,最简单的方法(我很乐意使用Express.js)是什么?我当前的服务器文件如下:Javascript 一个node.js服务器文件是否可以用于服务HTML页面和处理来自该页面的POST请求?,javascript,html,node.js,Javascript,Html,Node.js,我对运行Web服务器及其体系结构是完全陌生的。我目前正在构建一个web应用程序,它有一个基于HTML的GUI,使用一些JavaScript部分处理用户数据,然后将其作为POST请求发送到web服务器 我的问题很简单:同一个node.js服务器是否可以用于处理POST请求,或者是否需要两个不同的“服务器”(即两个不同的侦听器和端口) 如果是这样,最简单的方法(我很乐意使用Express.js)是什么?我当前的服务器文件如下: var express = require('express'), se
var express = require('express'),
serveStatic=require('serve-static'),
mysql = require('mysql');
var app = express();
app.use(serveStatic(__dirname));
var port = 8080;
app.listen(port, function() {
console.log('server listening on port ' + port);
});
app.post('/', function(req, res){
console.log('POST /');
console.dir(req.body);
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('thanks');
});
只要
if-else
用条件request.method==“POST”
阻止您需要的代码:
http = require('http');
fs = require('fs');
server = http.createServer( function(req, res) {
console.dir(req.param);
if (req.method == 'POST') { //-- Here Process POST requests
console.log("POST");
var body = '';
req.on('data', function (data) {
body += data;
console.log("Partial body: " + body);
});
req.on('end', function () {
console.log("Body: " + body);
});
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('post received');
}
else
{ //!!!----Here process HTML pages
console.log("GET");
//var html = '<html><body><form method="post" action="http://localhost:3000">Name: <input type="text" name="name" /><input type="submit" value="Submit" /></form></body>';
var html = fs.readFileSync('index.html');
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);
}
});
port = 3000;
host = '127.0.0.1';
server.listen(port, host);
console.log('Listening at http://' + host + ':' + port);
http=require('http');
fs=要求('fs');
server=http.createServer(函数(req,res){
控制台目录(请求参数);
if(req.method=='POST'){/--这里处理POST请求
控制台日志(“POST”);
变量体=“”;
请求开启(“数据”,功能(数据){
body+=数据;
console.log(“部分主体:+body”);
});
请求开启('end',函数(){
console.log(“Body:+Body”);
});
res.writeHead(200,{'Content-Type':'text/html'});
res.end(“收到的邮件”);
}
其他的
{/!!---这里处理HTML页面
console.log(“GET”);
//var html='Name:';
var html=fs.readFileSync('index.html');
res.writeHead(200,{'Content-Type':'text/html'});
res.end(html);
}
});
端口=3000;
主机='127.0.0.1';
监听(端口、主机);
log('在http://'+host+':'+port上侦听);
只要if else
用条件request.method==“POST”
阻塞您所需的代码:
http = require('http');
fs = require('fs');
server = http.createServer( function(req, res) {
console.dir(req.param);
if (req.method == 'POST') { //-- Here Process POST requests
console.log("POST");
var body = '';
req.on('data', function (data) {
body += data;
console.log("Partial body: " + body);
});
req.on('end', function () {
console.log("Body: " + body);
});
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('post received');
}
else
{ //!!!----Here process HTML pages
console.log("GET");
//var html = '<html><body><form method="post" action="http://localhost:3000">Name: <input type="text" name="name" /><input type="submit" value="Submit" /></form></body>';
var html = fs.readFileSync('index.html');
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);
}
});
port = 3000;
host = '127.0.0.1';
server.listen(port, host);
console.log('Listening at http://' + host + ':' + port);
http=require('http');
fs=要求('fs');
server=http.createServer(函数(req,res){
控制台目录(请求参数);
if(req.method=='POST'){/--这里处理POST请求
控制台日志(“POST”);
变量体=“”;
请求开启(“数据”,功能(数据){
body+=数据;
console.log(“部分主体:+body”);
});
请求开启('end',函数(){
console.log(“Body:+Body”);
});
res.writeHead(200,{'Content-Type':'text/html'});
res.end(“收到的邮件”);
}
其他的
{/!!---这里处理HTML页面
console.log(“GET”);
//var html='Name:';
var html=fs.readFileSync('index.html');
res.writeHead(200,{'Content-Type':'text/html'});
res.end(html);
}
});
端口=3000;
主机='127.0.0.1';
监听(端口、主机);
log('在http://'+host+':'+port上侦听);
“使用Express.js最简单的方法是什么?”既然您在更新问题后请求Express,我也需要更新答案:)对不起,我也非常乐意使用“原生”HTTP.js-我相信Express会更容易回答。我应该说“我可以使用express”“使用express.js最简单的方法是什么?”因为您在更新问题后请求express,我也需要更新答案:)对不起,我也非常乐意使用“原生”HTTP.js-我相信express会更容易回答。我应该说“我可以使用express”