I have a simple Nodejs server using express.
// App.js
const express = require("express");
const app = express();
app.get("/", (req, res, next) => {
res.status(200).send("Foo");
});
app.listen(3001);
When I am visiting localhost3001 in Chrome I see the anticipated output.
When I execute curl --request GET --write-out "%{http_code}\n" http://localhost:3001/ I get...
curl --request GET --write-out "%{http_code}\n" http://localhost:3001/
Foo200
When I was anticipating...
curl --request GET --write-out "%{http_code}\n" http://localhost:3001/ Foo200
I sent the output to a text file and observed no newline. Is there anyway to supress this newline?
Related
I have an Express GET route to fetch all users. Whenever a req.body is present, the server is always responding with 400.
Obviously, I have body-parser and this behaviour is only experienced in the production k8s environment (working fine in localhost)
Is there any limitation to send req.body to GET routes? Express documentation doesn't say so
You need to set the content type --header "Content-Type: application/json" in the GET request and it should work.
curl --header "Content-Type: application/json" --request GET --data '{"username":"xyz","password":"xyz"}' https://example.com/posttest
and here is the nodejs snippet
app.get('/posttest', function(req, res){
console.log("posttest call received")
res.send(req.body);
});
I have the curl command here which is working as I want:
curl -X POST -H "Content-Type: application/json" -d #cats.json http://localhost:8080
And I would like to get help on translating this into nodejs
I currently have localhost set up and my goal is to display data stored in a JSON file. I currently copy and paste the data into postman and post the data to the localhost but I want to not use postman. The curl command was useful but I want to incorporate it into my code.
I've tried
var request = require('request');
request.post({
headers: {'content-type' : 'application/json'},
url: 'http://localhost/8080',
body: "someData"
}, function(error, response, body){
console.log(body);
});
I've tried this but it doesn't provide the same outcome as the curl command does where the data appears in my localhost
In postman, you can export NodeJS request code out:
Click on Code
Then select NodeJS -> Request.
Copy to Clipboard
I am looking for simple example of exchange data between client (using curl) and Koajs server with Content-Encoding: gzip,deflate.
More detail: the client have log file. Client compress the log file and send the log file to server. Server decompress the log file and get the content.
Please give me both curl command and Koajs code.
Thank you.
When you wanna send compressed request to server using curl you can do so using
curl -v -H "Content-encoding: gzip" -X POST -H 'Content-Type:
application/json;charset=UTF-8' --data-binary #youdataFile.gzip
your_endpoint.com
and if you wanna decompress that request in koaServer, I don't really know about that but would surely like to know. As even I'm in need to know that
This is how I decompress data in Koa Server using zlib
var zlib = require('zlib');
zlib.unzip(buffer, (err, buffer) => {
if (err) return console.log(err);
var dataString = buffer.toString();
});
I'm trying to simulate a POST request to a server app based in Express for nodeJS. It's running on localhost:3000 (JS Fiddle: http://jsfiddle.net/63SC7/)
I use the following CURL line:
curl -X POST -d "{\"name\": \"Jack\", \"text\": \"HULLO\"}" -H "Content-Type: application/json" http://localhost:3000/api
but get the error message:
Cannot read property 'text' of undefined
Any ideas what I'm doing wrong?
Note: I can make a successful GET request using this line:
curl http://localhost:3000/api
Assuming you're trying req.body.text,
Have you used bodyParser?
app.use(express.bodyParser());
The answer by josh3736 is outdated. Express no longer ships with the body-parser middleware. To use it you must install it first:
npm install --save body-parser
Then require and use:
let bodyParser = require('body-parser');
app.use(bodyParser());
Just to update Thordarson's answer (which is now deprecated), the currently recommended way is:
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
See also https://stackoverflow.com/a/24330353/3810493
I am pretty new to node.js services and I am facing a problem with multipart/form-data content type. I need a way to disable body parser functionality for specific request.
I am using restify 2.6.1.
Below are some snippet of the configuration.
My setup is:
App.js :
server.use(restify.authorizationParser());
server.use(restify.dateParser());
server.use(restify.queryParser());
server.use(restify.jsonp());
server.use(restify.bodyParser());
server.use(restifyValidator);
server.use(restify.gzipResponse());
server.use(passport.initialize());
server.use(restify.conditionalRequest());
Route.js :
app.post({path: '/test/upload/:upload_image_name', version: ver}, uploadCtr.uploadImage);
app.post( {path: '/test/upload/:upload_image_name', version:ver }, passport.authenticate('bearer',{ session: false}),uploadCtr.uploadImage);
Without restify.bodyParser() the upload image is working( but everything which is relying on the json parser is failing )
Thanks in advance.
You shouldn't use bodyParser() for every route by default. In fact, you should only use bodyParser() for routes that require multipart uploads.
All servers using express.bodyParser are vulnerable to an attack which creates an unlimited number of temp files on the server, potentially filling up all the disk space, which is likely to cause the server to hang.
Demonstration
This problem is extremely easy to demonstrate. Here's a simple express app:
var express = require('express');
var app = express();
app.use(express.bodyParser());
app.post('/test', function(req, resp) {
resp.send('ok');
});
app.listen(9001);
Seems pretty innocuous right?
Now check how many temp files you have with something like this:
$ ls /tmp | wc -l
33
Next simulate uploading a multipart form:
$ curl -X POST -F foo=#tmp/somefile.c http://localhost:9001/test
ok
Go back and check our temp file count:
$ ls /tmp | wc -l
34
That's a problem.
http://andrewkelley.me/post/do-not-use-bodyparser-with-express-js.html
This problem also exists with Restify.
You can solve the problem by replacing .bodyParser() with:
server.use( restify.queryParser() );
server.use( restify.jsonBodyParser() );
But to answer your question about a particular route, you should move any middleware that you don't need for all routes into route specific middleware:
server.get('/route', restify.queryParser(), restify.jsonBodyParser(), routeHandler);
This can also take an array:
var routeMiddleware = [
restify.queryParser(),
restify.jsonBodyParser()
];
server.get('/route', routeMiddleware, routeHandler);