request to server node.js - javascript

I am learning about requests in node. I have created simple server
var http=require("http");
var fs=require("fs");
http.createServer(function(req,res){
switch(req.url){
case '/redirect' :
res.writeHead({"Location ":'/'});
res.end();
break;
case '/form.html' :
res.writeHead(200,{'Content-Type': 'text/html'});
req.setEncoding("utf-8");
var ws=fs.createReadStream("form.html")
var body="";
ws.pipe(body);
res.end(JSON.stringify(body));
break;
default:
res.writeHead(200,{"Content-Type": 'text/plain'});
res.end(JSON.stringify({
url:req.url,
method:req.method,
headers:req.headers
}));
}
}).listen(4001)
and request script
var request=require("request");
var inspect = require("util").inspect;
request('http://localhost:4001/form.html',function(err,res,body){
if(err){
throw err;
}
console.log(inspect({
err:err,
res:{
statusCode:res.statusCode
},
body:JSON.parse(body)
}))
});
If i understood it correctly (logic behind requests) =
1)the request script makes request(default get) to the server
2)The server script will notice request , and deterime what to do by request.url , here we are making request on /form.html
3) Server script will do /form/html case = put header into response, create read Stream and send the value into response
4) The response is sent into request script where it is represented by body argument in callback function
5) The script now do adction in callback function = console.log the response.
form.html contains simple htmp form , but why the output of request script is
{ err: null,
res: { statusCode: 200 },
body:
{ url: '/form.html',
method: 'GET',
headers: { host: 'localhost:4001', connection: 'close' } } }
and not (in body) the text code that contains form.html?

I'm not sure I understand exactly what you're trying to do in the form.html branch of your switch, but if you're just trying to serve the form.html file, you can do this:
case '/form.html' :
res.writeHead(200,{'Content-Type': 'text/html'});
var ws = fs.createReadStream("form.html");
ws.pipe(res);
break;
This will serve the contents of the local file form.html to the browser.

Related

client (fetch) and server (nodejs http) don't understand each other?

I try to wrap my mind around nodejs at the moment.
So I've created a client:
let myHeaders = {
'Content-Type': 'application/json',
'Accept': 'application/json'
};
let myBody = {
aString: "Test"
};
fetch("http://localhost:8099/", {
method: 'post',
mode: 'no-cors',
headers: myHeaders,
body: JSON.stringify(myBody)
})
.then(result => {
return result.text();
})
.then(text => {
// do stuff with text from server
});
And I have created a server:
// request needed modules
const http = require('http');
// init server
let server = http.createServer(logic);
server.listen(8099);
// server logic
function logic (req, res) {
var body = req.body;
res.end("Hello");
}
Two problems:
1) The sever does not get the body (req.body is undefined).
UPDATE
See my answer below.
--
2) The client does not receive "Hello" (result.text() returns "").
UPDATE
2 is solved by:
Changing this on the client
fetch("http://localhost:8099/", {
method: 'post',
mode: 'no-cors', <-- CHANGE to: mode: 'cors'
...
Adding this on server
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
--
I don't get what I do wrong here...
Your Node.js code has nothing which would generate the HTML document containing the JS that calls fetch.
This means you must be making a cross-origin request (this is supported by the absolute URL you pass to fetch).
You also set mode: 'no-cors' which means "Don't throw a security exception for trying to access a cross-origin resource, and don't make the response available to JS".
Consequently, when you try to read the response: You can't.
Set the mode to "cors" and change the Node.js code to follow the CORS specification to grant permission to the page trying to read the data.
I try to wrap my mind around nodejs at the moment.
There is nothing particular to Node.js here. The problems are related to security restrictions on what JavaScript running in the browser can do unless granted permission by the HTTP server.
To not completely mess up my question, I post my solution for problem number one as separate answer:
SOLUTION 1) The sever does not get the body (req.body is undefined)
As request is a stream, I need to treat it like one (notice "req.on('data'...)
This is how the server works as expected:
// request needed modules
const http = require('http');
// init server
let server = http.createServer(handler);
server.listen(8099);
// server logic
function handler (req, res) {
// Set CORS headers
let headers = {
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Methods' : 'POST, OPTIONS',
'Access-Control-Allow-Headers' : 'Content-Type, Accept'
};
res.writeHead(200, headers);
if(req.method == 'POST'){
var body = '';
req.on('data', data => {
body += JSON.parse(data).aString;
});
req.on('end', () => {
res.end(body.toString().toUpperCase());
});
} else if (req.method == 'OPTIONS'){
res.end();
}
}

MYSQL + Node.JS Post Request Confusion

I am very new to networking and I have this code which, when I use a REST API like Postman, does exactly what I want it to do:
router.post('/', function(req,res,next){
var reqObj = req.body;
console.log(reqObj);
req.getConnection(function(err, conn){
if(err)
{
console.error('SQL Connection error: ', err);
return next(err);
}
else
{
var query = conn.query("INSERT INTO coordinates (id,lat,lon) VALUES(3,2,1);");
if(err)
{
console.error('SQL error: ', err);
return next(err);
}
res.json("Coordinates sent.");
}
})
} );
That is, it sends the query request to the MYSQL database. My question is, how do I do this without using Postman to send the POST request?
Thank you.
You can't unless you make a post request from within your application or something. If you don't intend on sending data, you can just make it a GET request by changing
router.post('/', function(req,res,next){
to
router.get('/', function(req,res,next){
Then you can just go to the relevant URL from your browser. If you're using chrome and you just wanna see the JSON data, I'd also recommend installing the JSONView chrome extension.
EDIT
Here's the example request using request-promise
var request = require('request-promise');
var objectData = {
name: 'Bruce',
alias: 'Batman'
};
var options = {
method: 'POST',
uri: 'http://your.api/endpoint/',
body: objectData,
json: true // Automatically stringifies the body to JSON
};
request(options).then(function(response){
// handle success response
}, function(error){
// handle error response
})

Node.js: Cannot do PROFIND with request module

I'm trying to access content of my WebDAV server in node (server OK, I can net use it). I'm using the request module:
path = url.join(this._options.rootUrl, path || "");
var data =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
"<propfind xmlns=\"DAV:\">" +
"<propname/>" +
"</propfind>";
var headers = {
"Content-Type": "text/xml",
"Depth": depth || 1
};
var req = request({
url: path,
data: data,
headers: headers,
method: "PROPFIND",
json: false,
encoding: "utf8",
auth: {
user: this._options.user,
password: this._options.password
}
});
req.on("response", ...);
req.on("error", ...);
The problem is I'm getting HTTP 405 instead of the result. I've tried bundled https module, same result.
Is it possible that Node.js cannot invoke costom verbs like PROPFIND?
UPDATE 1:
This is stated that node can do PROFIND. So my question is, how do you people do correct WebDAV PROPFIND client request in node/io.js by using request module? (Or anything that works for this purpose.)
PROPFIND or any other verb is supported just fine for outbound requests in node and iojs. There is either something else wrong with your request (wrong/bad headers or payload for example) or configuration on the server.
Example of PROPFIND working:
// client.js
require('http').request({
host: '127.0.0.1',
port: 8000,
method: 'PROPFIND'
}, function(res) {
res.resume();
}).end();
// server.js
require('http').createServer(function(req,res) {
// echoes "PROPFIND" to stdout
console.log(req.method);
res.writeHead(200, { 'Connection': 'close' });
res.end();
}).listen(8000);

Piping images versus sending callback body in node.js with request

I'm using node.js 0.10.33 and request 2.51.0.
In the example below, I've built a simple web server that proxies image using request. There are two routes set up to proxy the same image..
/pipe simply pipes the raw request to the response
/callback waits for the request callback and send the response headers and the body to the response.
The pipe example works as expected but the callback route won't render the image. The headers and the body appear to be the same.
What about the callback route is causing the image to break?
Here is the example code:
var http = require('http');
var request = require('request');
var imgUrl = 'https://developer.salesforce.com/forums/profilephoto/729F00000005O41/T';
var server = http.createServer(function(req, res) {
if(req.url === '/pipe') {
// normal pipe works
request.get(imgUrl).pipe(res);
} else if(req.url === '/callback') {
// callback example doesn't
request.get(imgUrl, function(err, resp, body) {
if(err) {
throw(err);
} else {
res.writeHead(200, resp.headers);
res.end(body);
}
});
} else {
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.write('<html><head></head><body>');
// test the piped image
res.write('<div><h2>Piped</h2><img src="/pipe" /></div>');
// test the image from the callback
res.write('<div><h2>Callback</h2><img src="/callback" /></div>');
res.write('</body></html>');
res.end();
}
});
server.listen(3000);
Result in this
The problem is that body is a (UTF-8) string by default. If you're expecting binary data, you should explicitly set encoding: null in your request() options. Doing so will make body a Buffer, with the binary data intact:
request.get(imgUrl, { encoding: null }, function(err, resp, body) {
if(err) {
throw(err);
} else {
res.writeHead(200, resp.headers);
res.end(body);
}
});

nodejs http post request throws TypeError

I am trying to make a simple server that use google oauth (without express and passportjs, as I want to study the data exchanged).
When my program attempts to send a post request to google, nodejs throws:
http.js:593 throw new TypeError('first argument must be a string or Buffer');
I have checked and make sure that all parameters in query and option are all string, but the error still persist. What could I have missed here?
Here is my code:
// Load the http module to create an http server.
var http = require('http');
var url = require('url');
var fs = require('fs');
var querystring = require('querystring');
var content;
fs.readFile('./test.html',function(err,data){
content = data;
});
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
var path = url.parse(request.url).pathname;
var query = querystring.parse(url.parse(request.url).query);
var code;
if (query!=null) {
code = query.code;
};
if ('/auth/google/callback'==path){
var data = querystring.stringify({
'code': ''+code,
'client_id': 'id',
'client_secret': 'secret',
'redirect_uri': 'http://localhost:8999/auth/google/code/callback',
'grant_type': 'authorization_code'
});
var options = {
hostname: 'accounts.google.com',
port:'80',
path: '/o/oauth2/token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': ''+data.length
}
};
debugger;
var post = http.request(options, function(res){
response.write(res);
response.end();
});
debugger;
post.write(data);
debugger;
post.end();
}
else if (path=='/auth/google/code/callback'){
console.log(request.headers);
console.log(request.url);
}
else response.end(content);
console.log(request.headers);
console.log(request.url);
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8999);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");
Many thanks,
I think problem is when you are saying
response.write(res); //it needs a string
I think res is an object here.
try
response.write(JSON.stringify(res));
When you write response or request. It should contain string so you need to change it to
response.write(querystring.stringify(res));
or
response.write(JSON.stringify(res));

Categories