How to authenticate a request with htpasswd in Node.js - javascript

I am trying to fetch a rss feed from our staging site and at present it has htpasswd security on it.
I have tied using the format:
http://username:password#url.com
This works on the browser but when I try to do this with nodejs it fails.
Could you tell me what is the right way to do this with node.js.

app.js
var request = require('request');
request.get('http://url.com', callback).auth('username', 'password', false);
function callback(err, response, body) {
console.log(body);
}
terminal:
npm install request
node app.js
document: https://github.com/mikeal/request

Related

Node.- issues when trying to request behind https proxy

I am trying to download an image from a server through an https proxy, please help.
My code:
var request = require('request');
request({
url: url,
proxy: proxy
}, function (err, res, imgBuffer) {
console.log(err)
console.log(res)
})
The error:
Error: tunneling socket could not be established, cause=write EPROTO 101057795:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:openssl\ssl\s23_clnt.c:827:
I can provide any additional info needed, I have already tried a lot.
I'm thinking you need to change the config file in npm.
Check out this tutorial: https://www.jhipster.tech/configuring-a-corporate-proxy/
Go down to the section titled "NPM configuration."

Express.js piping request to php built-in server on localhost results in ECONNREFUSED

I am building a simple website using npm for development, and it is hosted with a provider with php support.
The only functionality that uses php is contact form to send email. the rest is simple html and javascript.
I use a simple php server in development started with php -S localhost:8000 to test a simple php email script and again in dev I reverse proxy requests for email.php to this php server locally.
Node app is on port 3000 and php server is on port 8000. The problem is I get connection refused error with the following express server configuration when request goes through localhost:3000/email.php:
#!/usr/bin/env node
var express = require('express');
var app = express(),
request= require('request'),
port = +(process.env.PORT || 3000);
app.set('case sensitive routing', false);
app.post( '/email.php', function( req, res ){
req.pipe( request({
url: 'http://localhost:8000/email.php',
qs: req.query,
method: req.method
}, function(error){
if (error.code === 'ECONNREFUSED'){
console.error('Refused connection');
} else {
throw error;
}
})).pipe( res );
});
// other request handlers here
app.listen(port, function() {
console.log('listening');
});
Php server is definitely up and serving all the pages on port 8000, which I can browse with the browser. I test it with curl and it seems to be handling the request just fine when posted directly to localhost:8000 using curl.
Not sure why I get this error, scratching my head, can't think of any reason.
Help is much appreciated.
I figured out what it was, d'oh! Well I am gonna post the answer in case someone else stumbles upon this.
PHP is to blame it seems; Checking the sockets listening a port using ss -ltn ( I am on Linux, this might not work for you) I realised php server is listening IPv6 only. Relevant output as follows:
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 ::1:8000
With the relevant search I found the answer on web server documentation page under user notes posted by a user. See the post here. The solution is to use 127.0.0.1 rather than localhost:
As it turned out, if you started the php server with "php -S
localhost:80" the server will be started with ipv6 support only!
To access it via ipv4, you need to change the start up command like
so: "php -S 127.0.0.1:80" which starts server in ipv4 mode only.

Retrieve JSON from HTTP POST request

I have the following function (essentially taken straight from the answer to another SO question):
function makePostRequest(requestURL, postData) {
request(
{
url: requestURL,
method: "POST",
json: true,
body: postData
},
function(error, response, body) {
console.log(response);
});
}
When I call it with the right requestURL, I successfully reach this route:
router.post("/batchAddUsers", function(req, res) {
console.log("Reached batchAddUsers");
});
I've been trying to retrieve the postData I sent with the request to no avail. Both req.params and req.body are {}. I haven't got the faintest clue how to refer to the object containing body passed in the request.
I read the whole console.log(req) and found nothing useful. I've done this kind of stuff before, except the request was made by a form and req.body worked like a charm. Now that I'm doing the request "manually", it doesn't work anymore. The whole thing is running on Node.js.
What am I doing wrong?
I think you did not insttalled body-parser
To handle HTTP POST request in Express.js version 4 and above, you need to install middleware module called body-parser.
body-parser extract the entire body portion of an incoming request stream and exposes it on req.body .
The middleware was a part of Express.js earlier but now you have to install it separately.
This body-parser module parses the JSON, buffer, string and url encoded data submitted using HTTP POST request. Install body-parser using NPM as shown below.
npm install body-parser --save

Communication with an http-server

I have some data that I want to store locally and to be able to pull it dynamically, maybe in another session or after the browser was closed and all browser data was cleared.
I run the site with http-server CLI command and navigate to localhost to access it from the browser.
How can I send data to the server side so the server side will save the data as a file?
I tried to do an ajax post request to see if something happens in the console, but it just returned 404 and nothing came up in the console.
The docs don't mention anything about post requests: https://www.npmjs.com/package/http-server
PS: I have to run this with http-server, this is an offline project.
You will not be able to do this with http-server alone, because http-server can only serve static content and cannot be used to run any code on the server side.
You will have to write a backend yourself, possibly using a framework like Express, Hapi, Restify, Loopback etc. and serve your static files that you need with your new backend, or keep it served as you do now but then you will probably need to take CORS into account if you use different ports for your data saving/retrieving endpoints and your static content - unless you run a reverse proxy that makes it all appear on the same host name and port.
You can use the file system to save the data or you can use a database - either a standalone database like Mongo or Postgres or an embedded database like SQLite or Loki.
For examples on how to serve static content in your own backend see:
How to serve an image using nodejs
You should use express for this kind of stuff. You can easily make methods that handle certain requests.
Here is an exmaple on how to handle a get request by just sending some data
var express = require('express')
var app = express()
app.get('/', function (req, res) {
res.send('Hello World')
})
app.listen(3000)
And you can use the fs api from node itself to write data.
var fs = require('fs')
fs.writeFile('message.txt', 'Hello Node.js', (err) => {
if (err) throw err;
console.log('It\'s saved!');
});
Note: the fs example uses arrow functions. You can find more information here

Node.js Server is timing out in browser but cURL request works

I am developing on a Ubuntu server that I do not own. This is my first node application.
Node.js has been installed on the server. I have created a simple server file:
Server.js
var http = require("http");
http.createServer(function (request, response){
response.writeHead(200, {'Content-Type': 'text/plain'});
//response to send out
response.end('Hello sof');
//print to screen
console.log('request processed\n');
}).listen(1234);
console.log("Server Running on 1234");
In putty I run the server with this command:
node Server.js
In another instance of putty I run this command:
curl http://my.url.website.ca:1234/*curl request*/
In the putty instance where I ran the curl command I get this output:
Hello sof
In the putty instance where I ran the node Server this get printed to the screen:
request processed
So it looks like the server is running. However when I put http://my.url.website.ca:1234 into my browser I get this error:
GET http://my.url.website.ca:1234/ net::ERR_CONNECTION_TIMED_OUT
and nothing is printed to the screen in putty window where the server is running.
Am I doing something wrong or missing something in my code? Or is this a configuration issue?
I linked the server person to this page and he confirmed that the issue was in fact the firewall. The port I was trying to use was not open. The port was then opened and I was able to connect through the browser.
I then tested it with my jquery application and had a CORS error. For anyone else running into the same issue as me you will probably have that issue next. I fixed it by adding these headers:
response.setHeader('Access-Control-Allow-Origin', 'http://my.website.ca');
response.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
response.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
Cheers!

Categories

Resources