In jQuery we can do the POST submit as below. But in NodeJS how do i do GET or POST submit? (as native as possible without involving extra modules and take dependencies risk for server)
$.post('http://aaaaa.com/ajax/emailit', {
action:'contactsubmit',,
},
function(msg) {},'json');
EDIT:
var db = [];
var io = require('socket.io').listen(3003);
io.set('log level', 1);
function PING() {
var options = {
hostname: 'www.google.com',
port: 80,
path: '/upload',
method: 'POST'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
}
io.sockets.on('connection', function (socket) {
PING();
});
ERROR:
mac-air:signal $ node a.js
info - socket.io started
/private/var/tmp/sip-phone/signalmaster/a.js:37
var req = http.request(options, function(res) {
^
ReferenceError: http is not defined
at get (/private/var/tmp/sip-phone/signalmaster/a.js:37:13)
at SocketNamespace.<anonymous> (/private/var/tmp/sip-phone/signalmaster/a.js:57:3)
at SocketNamespace.emit [as $emit] (events.js:117:20)
at connect (/private/var/tmp/sip-phone/signalmaster/node_modules/socket.io/lib/namespace.js:292:10)
at /private/var/tmp/sip-phone/signalmaster/node_modules/socket.io/lib/namespace.js:308:13
at SocketNamespace.authorize (/private/var/tmp/sip-phone/signalmaster/node_modules/socket.io/lib/namespace.js:252:5)
at SocketNamespace.handlePacket (/private/var/tmp/sip-phone/signalmaster/node_modules/socket.io/lib/namespace.js:302:14)
at Manager.handleClient (/private/var/tmp/sip-phone/signalmaster/node_modules/socket.io/lib/manager.js:698:32)
at Manager.handleUpgrade (/private/var/tmp/sip-phone/signalmaster/node_modules/socket.io/lib/manager.js:618:8)
at Server.<anonymous> (/private/var/tmp/sip-phone/signalmaster/node_modules/socket.io/lib/manager.js:123:10)
This is running:
var http = require('http');
var net = require('net');
var url = require('url');
var io = require('socket.io').listen(3003);
io.set('log level', 1);
function PING() {
var options = {
hostname: 'www.google.com',
port: 80,
path: '/upload',
method: 'POST'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
}
io.sockets.on('connection', function (socket) {
PING();
});
Output:
STATUS: 404
HEADERS: {"content-type":"text/html; charset=UTF-8","x-content-type-options":"nosniff","date":"Fri, 03 Oct 2014 04:59:45 GMT","server":"sffe","content-length":"1431","x-xss-protection":"1; mode=block","alternate-protocol":"80:quic,p=0.01"}
BODY: <!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 404 (Not Found)!!1</title>
<style>
*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/errors/logo_sm_2.png) no-repeat}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/errors/logo_sm_2_hr.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:55px;width:150px}
</style>
<a href=//www.google.com/><span id=logo aria-label=Google></span></a>
<p><b>404.</b> <ins>That’s an error.</ins>
<p>The requested URL <code>/upload</code> was not found on this server. <ins>That’s all we know.</ins>