我有一个php脚本,在脚本的开头使用$_server[“request_method”];
。 然后我需要检查什么是请求被发送像'GET','POST'等共享一些php脚本如下。
<?php
try {
$method_name = $_SERVER["REQUEST_METHOD"];
if ($_SERVER["REQUEST_METHOD"]) {
// more code
}
}
?>
我目前尝试的是创建一个新的服务器节点,如下所示。
var http = require('http');
http.createServer(function (req, res) {
console.log(req.method).
if(req.method === 'POST') {
// more code
}
}).listen(8080);
目前它不是作为我的php脚本工作,任何想法?
您的node.js代码有一个语法错误,但是您正在正确地检查方法。 您只需在通过res.end
完成请求时结束请求,这样浏览器就知道它得到了完整的响应。
var http = require('http');
http.createServer(function (req, res) {
console.log('Received "' + req.method + '" request!');
if (req.method === 'POST') {
res.end('This was a POST request');
return;
}
res.end('This was a GET request');
}).listen(8080);