Hey, I built ssl communication with self certificate. The connection is working, but when I trying to deliver data on the client side with post request in postman or my android client, the body of the request in the server side is empty.
Here is my nodejs server code below,thankes for help.
const express = require('express')
const https = require('https')
const path = require('path')
const fs = require('fs')
const app = express()
app.use(express.json())
app.post('/',(req,res,next)=>
{
console.log("we got new connection")
var data =req.body
res.send(data)
})
const ssl_server = https.createServer(
{
key:fs.readFileSync(path.join(__dirname,'cert','key.pem')),
cert:fs.readFileSync(path.join(__dirname,'cert','cert.pem'))
},app)
ssl_server.listen(3443,console.log("SSl server is online!"))
You're reading the response body. You should look at the request.
In any case, there won't probably be much reasonable data to read unless you parse the POST payload somehow; I'd recommend the Express library rather than raw Node.js HTTP bits if you don't want to do that yourself.
here is my answer for my question,like AKS said, you need to parse the post request, i add bodyparser to my code and it worked.
const express = require('express')
const https = require('https')
const path = require('path')
const fs = require('fs')
const bodyParser = require('body-parser');
const app = express()
app.use(bodyParser.urlencoded({ extended: true })) // <==== parse request body as JSON
app.post('/',(req,res,next)=>
{
console.log("we got new connection")
var data =req.body
res.send(data)
})
const ssl_server = https.createServer(
{
key:fs.readFileSync(path.join(__dirname,'cert','key.pem')),
cert:fs.readFileSync(path.join(__dirname,'cert','cert.pem'))
},app)
ssl_server.listen(3443,console.log("SSl server is online!"))
Related
I am testing a GET & POST methods using ThunderClient extension and NodeJs, but I am unable to post the data ...it is showing cannot post data followed by relative path , (Get method is working perfectly) :
Here is my Index.js code:
const express = require('express')
const bodyParser = require('body-parser');
const Blockchain = require('./Blockchain');
const blockchain = new Blockchain();
const app = express();
app.use(bodyParser.json());
app.get('/api/block' , (req,res)=>{
res.json(blockchain);
})
app.post('/api/mine' , (req,res)=>{
const {data} = req.body;
blockchain.addBlock({data});
res.redirect('/api/block');
})
const PORT = 3000;
app.listen(PORT , ()=>{
console.log("app is listening");
})
The default status code of the redirect is 302 which doesn't change
the request method.
Instead it stays as POST
To redirect as a GET method, you have to use 303 status code.
Also see
res.redirect
Temporary redirects
I have written a simple request response code in NODEJS but there is no response in return of the request is there .
The code for my app.js(Server file) is
const express = require('express');
const cors = require('cors')
const paymentroute = require('./routes/paymentRoutes');
const app = express();
app.use(cors);
app.use("/api",paymentroute);
app.listen(3100,()=>{
console.log(`listening to port 3100`);
})
The code for my req and res is
const express = require('express');
const router = express.Router();
// const { checkout } = require('../controllers/paymentController');
router.post("/checkout",(req,res) => {
console.log("this function is called ")
return res.json({success:"true"})
});
module.exports = router;
Even the console.log inside the res function is not working.
Just change app.use(cors); to app.use(cors());
const express = require('express');
const cors = require('cors');
const paymentroute = require('./routes/paymentRoutes');
const app = express();
app.use(cors());
app.use('/api', paymentroute);
app.listen(3100, () => {
console.log(`listening to port 3100`);
});
I think it is a connection-related issue often related to a networking issue caused by a Virtual Private Network (VPN) that in some cases is required by some API services or, in other cases, prevents you to reach an API service.
The issue seems to be a combo of having the no-cache header enabled and a request URL length over 64 characters. If the issue persists after doing this solution then try to upgrade to the latest version of Postman.
I'm trying to deploy my Express API in my EC2. But I keep getting this error "ERR_SSL_PROTOCOL_ERROR" and despite my researches, I cannot find the right thing to do to fix my error.
My front end is on apache and requesting information through PUT/GET request toward the API. I've open the port on AWS and updated default-ssl.conf apache's file :
const express = require('express')
const axios = require('axios');
var cors = require('cors')
const app = express();
const fs = require('fs');
const bodyParser = require('body-parser');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const execSync = require('child_process').execSync;
app.use(cors())// Authorize navigator calls
app.use(bodyParser.urlencoded({ extended: true }));
// We send the rarity table
app.get("/rarity", async (req, res) => {
try {
let nft_json = JSON.parse(fs.readFileSync('rarity.json', 'utf-8'));
// We gather the information regarding this NFT to mint it
console.log(nft_json);
res.send(nft_json);
}
catch (err) {
console.log("error rarity table")
}
})
app.listen(8080, () => {
console.log("Serveur à l'écoute")
})
ServerName www.xx.com
SSLEngine on
ProxyPass /rarity https://www.xx.com:8080
ProxyPassReverse /rarity https://www.xx.com:8080
I can access the express /rarity through http but not https. What am I doing wrong in my default-ssl.conf ?
Have a great day,
regards
I have a firebase post function that I am trying to send some data to but the problem I am having is that I the body comes empty. I am using express for the function.
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const express = require("express");
const request = require("request");
const moment = require("moment");
const cors = require("cors");
// const bodyParser = require("body-parser");
admin.initializeApp();
const app = express();
app.use(cors());
app.use(express.json({limit: "10kb"}));
// Mpesa Express
app.post("/express", _accessToken, (req, res) => {
console.log(JSON.stringify("FELOOOOOO", req.body));
console.log(JSON.stringify("felix"));
res.status(200).json(req.body);
});
exports.main = functions.https.onRequest(app);
The functions run but when I log the req.body it comes empty. I have used postman to send the post request but still, the req.body comes empty despite passing in some data. Kindly help solve the error. below is a screenshot of the postman request
You seem to be sending the data correctly but are incorrectly using JSON.stringify(). The object is the first argument, not the second. Make it JSON.stringify(req.body) - that should work.
You can read up more about the function over at MDN docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
We first developed REST api's using node.js and have that running on a VPS. We now have developed an Angular web app to display data that comes in via a mobile app that calls some of the REST api's and then updates the data by calls back to other REST API's. Running the Angular app on the localhost was able to successfully call the REST api's.
We want to combine both applications on the same server. After searching around it seemed that we could add commands to the REST api server.js to pass urls that didn't meet the REST api path to Angular. Code snippet is below:
// API verison
var apiVersion = '/v1'
var fs ;
var https ;
// Dependencies
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
// MongoDB
...
// Express
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Routes
app.use(apiVersion, require('./routes/api'));
// Start server
fs = require('fs')
https = require('https')
https.createServer({
key: fs.readFileSync('...'),
cert: fs.readFileSync('...')
}, app)
.listen(443, function () {
console.log('HTTPS Server running on default port 443')
});
// Pass request to angular?
app.use(function(req, res) {
var path = require('path');
res.sendfile(path.resolve('/home/.../index.html')); // but not getting anything
});
The REST api's still work but when directing a browser to 'mydomain.net' I just get a blank page. I don't see any errors on the node/error logs.
Any ideas?
You can do something like this. Use static content to send from the dist folder and rest will work fine. I have added two reference in case you might need them to refer.
var apiVersion = '/v1'
const fs = require('fs')
const https = require('https');
const path = require('path')
// Dependencies
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
// MongoDB
...
// Express
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Pass request to angular?
app.use('path', express.static(path.resolve('your path should be correct')));
// Routes
app.use(apiVersion, require('./routes/api'));
// Start server
https.createServer({
key: fs.readFileSync('...'),
cert: fs.readFileSync('...')
}, app)
.listen(443, function () {
console.log('HTTPS Server running on default port 443')
});
Check here1 or here2 for more details, there are simple steps to follow.