I want to debug the client to nodejs server, print the http request original message like curl. any one can help me?
curl -vs -o /dev/null http://127.0.0.1:8080
var app = require('http').createServer(function (request, response) {
// print http orignal message like curl
});
app.listen(3000);
//express
var app=express();
app.use(function(req,res,next){ // print http orignal message like curl
console.log(req.url)
console.log(req.headers)
console.log(req.query)
console.log(req.body)
next();
});
So, not clear what you mean... but this should work (without express):
var app = require('http').createServer(function (request, response) {
// print http orignal message like curl
// request.method, URL and httpVersion
console.log(request.method + ' ' + request.url + ' HTTP/' + request.httpVersion);
// request.headers
for (var property in request.headers) {
if (request.headers.hasOwnProperty(property)) {
console.log(property + ': ' + request.headers[property])
}
}
});
app.listen(3000);
Or this one with express middleware:
const express = require('express')
const app = express();
// middleware to track request message
app.use(function (req, res, next) {
console.log(req.method + ' ' + req.url + ' HTTP/' + req.httpVersion);
for (var property in req.headers) {
if (req.headers.hasOwnProperty(property)) {
console.log(property + ': ' + req.headers[property])
}
}
next();
});
// your routes
app.get('/', function (req, res, next) {
res.send('Hello World');
});
app.listen(3000);
Related
This may be a somewhat stupid question but how would I pass a url into express url params array. When I try a youtube link half of it is cut off. For example 'https://www.youtube.com/watch?v=iik25wqIuFo' comes out as 'https://www.youtube.com/watch'. Thanks for any help. Heres my code:
const express = require('express');
const url = require('url');
const app = express();
const fs = require('fs')
const youtubedl = require('youtube-dl')
function downloadvideo(link) {
const video = youtubedl(link,
['--format=18'],
{ cwd: __dirname })
video.on('info', function(info) {
console.log('Download started')
console.log('filename: ' + info._filename)
console.log('size: ' + info.size)
})
video.pipe(fs.createWriteStream('myvideo.mp4'))
};
app.get('/', function (req, res) {
res.send('Hello Express app!');
});
app.get('/download/video/:id', function (req, res) {
app.use(express.urlencoded({ extended: true }));
res.send('Downloading ' + req.params.id);
});
app.get('/download/subtitle/:id', function (req, res) {
res.send('user ' + req.params.id);
});
app.get('/download/playlist/:id', function (req, res) {
res.send('user ' + req.params.id);
});
app.listen(3000, () => {
console.log('server started');
});
To pass in text with special characters like ?, you must first run it through a function like encodeURIComponent() which essentially makes these special characters' URL safe. So first step is to make the parameter URL safe by running it through encodeURIComponent() (or you could escape special characters manually which is not fun):
console.log(encodeURIComponent('https://www.youtube.com/watch?v=iik25wqIuFo'));
// result: "https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Diik25wqIuFo"
Then pass it into your URL as a query parameter.
yourAppUrl.com/path?youtubeLink=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Diik25wqIuFo
I'm trying to using express().router as middleware for express server.
this is the code for the express server -
const server = express();
const port = process.env.PORT || 5000;
server.use(logger("short"));
server.use(respomnseTime(4)) // The number indicate how digits we want
server.use('/', mRouter);
server.use((request, response, next)=>{
mUtil.print("Request IP: " + request.url);
mUtil.print("Request Date: " + new Date());
next();
})
server.use((request, response, next)=>{
mUtil.auth(request, response, next);
})
server.use((request, response, next)=>{
console.log("Middleware 2");
next();
})
this is the code for the router (another file) -
const router = require('express').Router();
router.get('/', (request, response) => {
response.send({ express: 'YOUR EXPRESS BACKEND IS CONNECTED' });
});
router.get('/user/:username', (request, response)=>{
});
router.get('/about', (request, response)=>{
});
router.get('/contact', (request, response)=>{
});
It looks like he middlewares after -
server.use('/', mRouter);
not running. I thought the problem happens because there is no call to next() function, but I'm not sure how to write it. Anyone have an idea?
you should export your router like below
module.exports = router;
below node.js code is using express to route to different urls, how can I do the same with http instead of express?
var express = require('express');
var app = express();
app.use(express.static('public'));
app.get('/', function (req, res) {
res.send('Welcome Home');
});
app.get('/tcs', function (req, res) {
res.send('HI RCSer');
});
// Handle 404 - Keep this as a last route
app.use(function(req, res, next) {
res.status(404);
res.send('404: File Not Found');
});
app.listen(8080, function () {
console.log('Example app listening on port 8080!');
});
Here's a quick example. Obviously, there are many ways of doing this, and this is probably not the most scalable and efficient way, but it will hopefully give you an idea.
const http = require('http');
const fs = require('fs');
const server = http.createServer((req, res) => {
req.on('error', err => {
console.error(err);
// Handle error...
res.statusCode = 400;
res.end('400: Bad Request');
return;
});
res.on('error', err => {
console.error(err);
// Handle error...
});
fs.readFile('./public' + req.url, (err, data) => {
if (err) {
if (req.url === '/' && req.method === 'GET') {
res.end('Welcome Home');
} else if (req.url === '/tcs' && req.method === 'GET') {
res.end('HI RCSer');
} else {
res.statusCode = 404;
res.end('404: File Not Found');
}
} else {
// NOTE: The file name could be parsed to determine the
// appropriate data type to return. This is just a quick
// example.
res.setHeader('Content-Type', 'application/octet-stream');
res.end(data);
}
});
});
server.listen(8080, () => {
console.log('Example app listening on port 8080!');
});
Try the code below . It is a pretty basic example
var http = require('http');
//create a server object:
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'}); // http header
var url = req.url;
if(url ==='/about'){
res.write('<h1>about us page<h1>'); //write a response
res.end(); //end the response
}else if(url ==='/contact'){
res.write('<h1>contact us page<h1>'); //write a response
res.end(); //end the response
}else{
res.write('<h1>Hello World!<h1>'); //write a response
res.end(); //end the response
}
}).listen(3000, function(){
console.log("server start at port 3000"); //the server object listens on port 3000
});
express can also be used by http directly.
From: https://expressjs.com/en/4x/api.html#app.listen
The app returned by express() is in fact a JavaScript Function, designed to be passed to Node’s HTTP servers as a callback to handle requests.
var http = require('http')
var express = require('express')
var app = express()
http.createServer(app).listen(80)
With this, you can still make use of express for routing, while keeping native http support.
I use the following code to accept the user sms from my android app and send back the result to the user after making specified get request to some site.the expected output that the user should get is "thanks for your message"+[the response of get request]..what i get is "Thanks for your message undefined"it seems that my variable "body" doesnt get initialized with the GET response.please help
var express = require('express');
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.get('/', function(request, response) {
response.send('Hello Cruel World!');
});
var bodyParser = require('body-parser');
var WEBHOOK_SECRET = "62DZWMCCFFHTTQ44CG3WUQ94CTT7GAAN";
app.post('/telerivet/webhook',
bodyParser.urlencoded({ extended: true }),
function(req, res) {
var secret = req.body.secret;
if (secret !== WEBHOOK_SECRET) {
res.status(403).end();
return;
}
if (req.body.event == 'incoming_message') {
var content = req.body.content;
var from_number = req.body.from_number;
var phone_id = req.body.phone_id;
var request = require("request");
var body;
request("http://www.google.com", function(error, response, data) {
body = data;
});
// do something with the message, e.g. send an autoreply
res.json({
messages: [
{ content: "Thanks for your message! " + body}
]
});
}
res.status(200).end();
}
);
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
please help me to resolve the problem..the answer posted here doesnt seems working http://goo.gl/GYgd6Z,help by taking my code specified here as example...please
The res.json line will execute before the callback from request, so body won't be populated - change like this:
request("http://www.google.com", function(error, response, data) {
// do something with the message, e.g. send an autoreply
res.json({
messages: [
{ content: "Thanks for your message! " + data}
]
});
res.status(200).end();
});
This ensures that the res.json is executed after the response from request().
I've written a small proxy with nodejs, express and htt-proxy. It works well for serving local files but fails when it comes to proxy to external api:
var express = require('express'),
app = express.createServer(),
httpProxy = require('http-proxy');
app.use(express.bodyParser());
app.listen(process.env.PORT || 1235);
var proxy = new httpProxy.RoutingProxy();
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html');
});
app.get('/js/*', function(req, res) {
res.sendfile(__dirname + req.url);
});
app.get('/css/*', function(req, res) {
res.sendfile(__dirname + req.url);
});
app.all('/*', function(req, res) {
req.url = 'v1/public/yql?q=show%20tables&format=json&callback=';
proxy.proxyRequest(req, res, {
host: 'query.yahooapis.com', //yahoo is just an example to verify its not the apis fault
port: 8080
});
});
The problem is that there is no response from the yahoo api, maybe there is an response but i dont came up in the browser.
Even simpler with pipe and request-Package
var request = require('request');
app.use('/api', function(req, res) {
var url = apiUrl + req.url;
req.pipe(request(url)).pipe(res);
});
It pipes the whole request to the API and pipes the response back to the requestor. This also handles POST/PUT/DELETE and all other requests \o/
If you also care about query string you should pipe it as well
req.pipe(request({ qs:req.query, uri: url })).pipe(res);
Maybe your code is different when you're testing, but I'm querying the same URL as in your code sample using the following:
http://query.yahooapis.com:8080/v1/public/yql?q=show%20tables&format=json&callback=
and I get nothing back. My guess is you want to change port to 80 (from 8080) -- it works when I change it like so:
http://query.yahooapis.com:80/v1/public/yql?q=show%20tables&format=json&callback=
So that means it should be:
proxy.proxyRequest(req, res, {
host: 'query.yahooapis.com', //yahoo is just an example to verify its not the apis fault
port: 80
});
Maybe I use http-proxy in a wrong way. Using restler does what I want:
var express = require('express'),
app = express.createServer(),
restler = require('restler');
app.use(express.bodyParser());
app.listen( 1234);
app.get('/', function(req, res) {
console.log(__dirname + '/index.html')
res.sendfile(__dirname + '/index.html');
});
app.get('/js/*', function(req, res) {
res.sendfile(__dirname + req.url);
});
app.get('/css/*', function(req, res) {
res.sendfile(__dirname + req.url);
});
app.all('/*', function(req, res) {
restler.get('http://myUrl.com:80/app_test.php/api' + req.url, {
}).on('complete', function (data) {
console.log(data)
res.json(data)
});
});
I ended up using http-proxy-middleware.
The code looks something like this:
var express = require("express");
var proxy = require("http-proxy-middleware");
const theProxy = proxy({
target: "query.yahooapis.com",
changeOrigin: true,
});
app.use("/", theProxy);
app.listen(process.env.PORT || 3002);
That's what I've been using for a while. Can handle both JSON and binary requests.
app.use('/api', (req, res, next) => {
const redirectUrl = config.api_server + req.url.slice(1);
const redirectedRequest = request({
url: redirectUrl,
method: req.method,
body: req.readable ? undefined : req.body,
json: req.readable ? false : true,
qs: req.query,
// Pass redirect back to the browser
followRedirect: false
});
if (req.readable) {
// Handles all the streamable data (e.g. image uploads)
req.pipe(redirectedRequest).pipe(res);
} else {
// Handles everything else
redirectedRequest.pipe(res);
}
});