I wrote an HTTPS NodeJS server (with a self signed certificate) where I want to receive and process POST requests from cURL CLI.
My server:
var https = require('https');
var fs = require('fs');
var config = require('./conf.json');
var g3updater_kernel = require('./g3updater_kernel');
var express = require('express');
var app = express();
var privateKey = fs.readFileSync(config.security_repository+'/CA/rootCA.key', 'utf8');
var certificate = fs.readFileSync(config.security_repository+'/CA/rootCA.pem', 'utf8');
var httpsOptions = {
key: privateKey,
cert: certificate
}
app.post('/', function(req, res){
console.log(req.query);
});
https.createServer(httpsOptions, app).listen(443);
cURL command I use:
curl -d M=1 https://localhost/ -k
The problem is that I receive an empty query. console.log(req.query) displays:
{}
am I missing something in the query?
In order to parse the queries in your post request you're going to want to use body-parser.
In Express 4, there was conscious decision made to move bodyParser and CookieParser to separate modules.
In order to access your queries, you're code should look something like:
var https = require('https');
var fs = require('fs');
var config = require('./conf.json');
var g3updater_kernel = require('./g3updater_kernel');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var privateKey = fs.readFileSync(config.security_repository+'/CA/rootCA.key', 'utf8');
var certificate = fs.readFileSync(config.security_repository+'/CA/rootCA.pem', 'utf8');
var httpsOptions = {
key: privateKey,
cert: certificate
}
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
app.post('/', function(req, res){
console.log(req.body);
});
https.createServer(httpsOptions, app).listen(443);
Note that they will be held in req.body instead of req.query.
Happy Coding!
Related
I can get bitbucket to run at localhost:7990 just fine however I wanted to secure it with a SSL and also remove the port from the address. When I attempt to do this with the code below and my node server I just simply get the 'CANNOT GET /' error if I goto https://example.com. Even though the bitbucket app is running.
// Dependencies
const fs = require('fs');
const path = require('path');
const http = require('http');
const https = require('https');
const express = require('express');
const express_force_ssl = require('express-force-ssl');
const app = express();
// Certificate
const privateKey = fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem', 'utf8');
const certificate = fs.readFileSync('/etc/letsencrypt/live/example.com/fullchain.pem', 'utf8');
const ca = fs.readFileSync('/etc/letsencrypt/live/example.com/chain.pem', 'utf8');
const credentials = {
key: privateKey,
cert: certificate,
ca: ca
};
app.use(express_force_ssl);
// Load main application
app.use(express.static(path.join(__dirname, '../../var/www/bitbucket/')));
// Starting both http & https servers
const httpServer = http.createServer(app);
const httpsServer = https.createServer(credentials, app);
httpServer.listen(80, (req,res) => {
console.log('HTTPS Server running on port 80');
});
httpsServer.listen(443, (req, res) => {
console.log('HTTPS Server running on port 443');
});
I want to handle the POST request data in NODEJS using HTTPS. Something like below,
var express = require('express')
var bodyParser = require('body-parser')
var https = require('https');
// Create a new instance of express
var app = express()
// Tell express to use the body-parser middleware and to not parse extended bodies
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }))
//private key and certificate for secure connection (https)
var options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem'),
requestCert: false,
rejectUnauthorized: false,
};
var httpsApp = https.createServer(options,app);
// Route that receives a POST request to /sms
httpsApp._events.request.post('/xyz', function (req, res) {
-----
some code
-----
})
httpsApp.listen(5004, function(){
console.log("listening on port 5004");
logger.info("listening on port 5004");
});
But turns out, this only works for HTTP request. This will not give any error but it wont receive the data to be received in '/xyz'.
Thanks.
Maybe you can use something like an Reverse-Proxy to fix this more nicely?
Maybe you need to check your certificates.
I have checked this code through my certificates and they are working with https.
I m putting code here:
var express = require('express')
var bodyParser = require('body-parser')
var https = require('https');
var fs = require('fs')
// Create a new instance of express
var app = express()
// Tell express to use the body-parser middleware and to not parse extended bodies
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }))
//private key and certificate for secure connection (https)
var options = {
key: fs.readFileSync('./certificates/server.key'),
cert: fs.readFileSync('./certificates/server.crt'),
requestCert: false,
rejectUnauthorized: false,
};
var httpsApp = https.createServer(options,app);
// Route that receives a POST request to /sms
httpsApp._events.request.get('/xyz', function (req, res) {
console.log("Works")
res.send("working")
})
httpsApp.listen(5004, function(){
console.log("listening on port 5004");
});
I try to configure my node server to SSL, my node server and php both work on the same instance. node is start without error but when i send a request though the socket or web url, it will crashed. only show
segmentation Fault
my node version is 6.9.4
here my Node js script
var https = require('https');
var http = require('http');
var fs = require('fs');
var app = require('express')();
var f_root = 'path_to_ssl';
var options = {
key: fs.readFileSync(f_root+'ssl.myserver.key'),
cert: fs.readFileSync(f_root+'ssl.myserver.pem')
};
app.get("/", function(request, response){
console.log(" Hello World");
});
var httpsserver = https.createServer(options, app);
httpsserver.listen(3001);
Finally found the issue
issue was the .pem file. i use the .crt instead of .pem file
here the update
var https = require('https');
var http = require('http');
var fs = require('fs');
var app = require('express')();
var f_root = 'path_to_ssl';
var options = {
key: fs.readFileSync(f_root+'ssl.myserver.key'),
// this is the issue
cert: fs.readFileSync(f_root+'ssl.myserver.crt')
};
app.get("/", function(request, response){
console.log(" Hello World");
});
var httpsserver = https.createServer(options, app);
httpsserver.listen(3001);
I am using node.js to index a file into elasticsearch.
i am trying to give input as a file from jmeter? i.e.., as req.file
but it giving output as undefined.
can anyone specify the better solution?
var http = require('http');
var express = require('express');
var app = express();
var CircularJSON = require('circular-json');
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: '10.96.82.98:9200',
log: 'trace'
});
http.createServer(app).listen(1000);
//indexing a document
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.post('/post/:index/:type/:id/', function(req, res){
var fs = require("fs");
fs.writeFile('inpu.file',req.file);
client.index({
index: req.params.index,
type: req.params.type,
id: req.params.id,
body:JSON.parse(fs.readFileSync(__dirname +'/inpu.file'))
},function (error, response) {
console.log(response);
res.send(response);
});
});
i am using this code
I have four pages inside a folder "public". I want to open that pages with HTTPS beacause getusermedia() no longer works in HTTP connections. This is my code:
var express = require("express");
var https = require('https');
var BinaryServer = require('binaryjs').BinaryServer;
var fs = require('fs');
var options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
var app = express();
var server = https.createServer(options, app);//.listen(9000);
app.use(express.static('public'));
app.listen(9000);
But the pages doesn't open in HTTPS (just open in HTTP). Before I check the certificate I want to be sure that everything is ok with the code.
You shouldnt use app.listen, as this is still only the "regular" express app (aka http) I believe.
This should point you in the right direction (taken from the API docs # http://expressjs.com/en/api.html)
Edit:
Added in how your code should look:
var express = require("express");
var https = require('https');
var BinaryServer = require('binaryjs').BinaryServer;
var fs = require('fs');
var options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
var app = express();
app.use(express.static('public'));
https.createServer(options, app).listen(9000);