I'm building a web application that processes a post request then performs a POST request to another server, and then redirects the user based on the returned information.
End result is user types in their username and clicks submit --> application process the post, takes the username --> application performs post to external server including the username --> server returns the url of the server the user should be on --> application redirects the user to that application.
server.js
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var findUser = require('./findUserInstance')
// Create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.use(express.static('public'));
app.get('/index.htm', function (req, res) {
res.sendFile( __dirname + "/" + "index.htm" );
})
app.post('/process_post', urlencodedParser, function (req, res) {
// Prepare output in JSON format
response = {
username:req.body.username
};
var uUrl = findUser.url(response.username);
console.log("redirecting to " + uUrl);
res.redirect(findUser.url(response.username));
res.end(JSON.stringify(response));
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("App listening at http://%s:%s", host, port)
})
findUserInstance.js
exports.url = function(uName) {
var http = require("https");
var uUrl;
var options = {
"method": "POST",
"hostname": "removed",
"port": null,
"path": "removed",
"headers": {
"appkey": "removed",
"content-type": "application/json",
"cache-control": "no-cache",
"Accept": "application/json",
"postman-token": "7d87bcf1-8e11-9717-2f6e-8150a5625acd"
}
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
var jsoncontent = JSON.parse(body);
uUrl = jsoncontent.rows[0].url;
console.log("The following should be: user.instance.url.com)
console.log(jsoncontent.rows[0].url);
return uUrl; //The information that I want to return to server.js
});
});
req.write(JSON.stringify({username: uName}));
req.end();
}
The problem is with returning the information from the external post module to the server.js module so that it can perform the redirect. Currently I have the variable uUrl (which is correctly populated with the URL from the post) returned from the function. However the findUserInstance module returns null.
How can I get the value of uUrl from the findUserInstance module to the server.js module?
#bryan euton good response you should return any object in findUserInstance like promise!
https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Promise
exports.url = function(uName) {
return new Promise( function (resolve, reject){
var http = require("https");
var uUrl;
var options = {
"method": "POST",
"hostname": "removed",
"port": null,
"path": "removed",
"headers": {
"appkey": "removed",
"content-type": "application/json",
"cache-control": "no-cache",
"Accept": "application/json",
"postman-token": "7d87bcf1-8e11-9717-2f6e-8150a5625acd"
}
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
var jsoncontent = JSON.parse(body);
uUrl = jsoncontent.rows[0].url;
console.log("The following should be: user.instance.url.com)
console.log(jsoncontent.rows[0].url);
resolve(uUrl); //The information resolve promise with your datas
});
});
req.write(JSON.stringify({username: uName}));
req.end();
});
}
Yes now uurl in server.js is asynchronous change handler:
app.post('/process_post', urlencodedParser, function (req, res) {
// Prepare output in JSON format
response = {
username:req.body.username
};
findUser.url(response.username).then( function(uUrl){
console.log("redirecting to " + uUrl);
res.redirect(findUser.url(response.username));
res.end(JSON.stringify(response));
});
});
Related
I start learning Node.js and Express.js and I'm trying to create a simple API to list data from JSON file (using the GET method) and add a new user using the POST method.
the GET method works fine but the POST method does not work
when I request http://127.0.0.1:8080/listusers the API sends all users in a JSON file.
when I request http://127.0.0.1:8080/adduser the API has to add new User Info and send the new data back to the browser.
NOTE: I read all the questions on Stackoverflow about this problem but
non of them help me so I have to ask again.
the problem is when I request http://127.0.0.1:8080/adduser I get the following error
Cannot GET /adduser
here is the server.js:
var express = require('express');
var app = express();
var fs = require('fs');
var user = {
"user4" : {
"name" : "mounir",
"password" : "password4",
"profession" : "teacher",
"id": 4
}
};
app.post('/adduser', function (req, res) {
// First read existing users.
fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
data = JSON.parse( data );
data["user4"] = user["user4"];
console.log( data );
res.end(JSON.stringify(data) );
});
});
app.get('/listusers', function (req, res) {
fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
console.log(data);
res.end(data);
});
});
var server = app.listen(8080, function () {
var host = server.address().address;
var port = server.address().port;
console.log("listening at http://%s:%s", "0.0.0.0", port)
});
The answer is in the error. Cannot GET /adduser. Keyword GET! If you are making a post request, be sure you include the appropriate headers and that you are making a POST request, with a body, and not a GET request. For instance if you are using fetch:
const myInit = {
method: 'POST',
headers: myHeaders,
body: {
...
}
};
fetch("http://127.0.0.1:8080/adduser", myInit)
.then(res => {
...
});
I am trying to make a newsLetter service using NodeJS & Express by using mailchimp API on hyper shell. I have installed all necessary things including npm,express,request,https module. The code works fine untill when i try to write the user Information in the mailChimp server and showing me the typeError message: request.write() is not a function. Below is my code & the snap of my errorCode.
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const request = require("request");
const https = require("https");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.get("/", function (req, res) {
res.sendFile(__dirname + "/signup.html");
});
app.post("/", function (req, res) {
const firstName = req.body.fname;
const lastName = req.body.lname;
const email = req.body.email;
const password = req.body.pass;
const cPassword = req.body.cPass;
//console.log(firstName);
//res.send(firstName);
var data = {
members: [
{
email_address: email,
status: "subscribed",
merge_fields: {
FNAME: firstName,
LNAME: lastName
}
}
]
};
const jsonData = JSON.stringify(data);
const url = "https://us1.api.mailchimp.com/3.0/lists/97d15bb1ff";
const options = {
method: "POST",
auth: "Mr.M:d2f2f965b9e6b751a305bb6ce2ad7ed4-us1",
};
https.request(url, options, function (response) {
response.on("data", function (data) {
console.log(JSON.parse(data));
});
});
request.write(jsonData);
request.end();
//res.send("hey")
});
app.listen(3000, function () {
console.log("Server is running at port 3000");
});
Error Message Picture
res.write(jsonData)
res.end()
use above code instead of request.write(jsonData), request.end().
https.request(url, options, function (response) {
response.on("data", function (data) {
console.log(JSON.parse(data));
}); });
instead use this:
const request = https.request(url, options, function (response) {
response.on("data", function (data) {
console.log(JSON.parse(data));
});
});
As already specified by Lingyu Kong, you need to save your request in a constant variable that will allow you to call upon it later:
The node.js website link below illustrates this perfectly:
https://nodejs.org/api/http.html#http_http_request_url_options_callback
You should have saved the ClientRequest into a variable called request, and after that, you could do the write and end.
Like that :
const request = https.request(url, options, function (response) {
response.on("data", function (data) {
console.log(JSON.parse(data));
});
You forgot to put in
const request = https.request(url, options, function(response) {
response.on("data", function(data) {
console.log(JSON.parse(data));
replace the code below:
https.request(url, options, function(response)
with this code:
const request = https.request(url, options, function(response)
Currently I'm using the below code to connect with web service.
I need to connect to the Microsoft Azure Machine Learning Studio Api by using either Vue Axios or Express. Can someone help me.
var http = require("http");
var https = require("https");
var querystring = require("querystring");
var fs = require('fs');
function getPred(data) {
console.log('===getPred()===');
var dataString = JSON.stringify(data)
var host = 'ussouthcentral.services.azureml.net'
var path = '/workspaces/fda91d2e52b74ee2ae68b1aac4dba8b9/services/1b2f5e6f99574756a8fde751def19a0a/execute?api-version=2.0&details=true'
var method = 'POST'
var api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=='
var headers = {'Content-Type':'application/json', 'Authorization':'Bearer ' + api_key};
var options = {
host: host,
port: 443,
path: path,
method: 'POST',
headers: headers
};
console.log('data: ' + data);
console.log('method: ' + method);
console.log('api_key: ' + api_key);
console.log('headers: ' + headers);
console.log('options: ' + options);
var reqPost = https.request(options, function (res) {
console.log('===reqPost()===');
console.log('StatusCode: ', res.statusCode);
console.log('headers: ', res.headers);
res.on('data', function(d) {
process.stdout.write(d);
});
});
// Would need more parsing out of prediction from the result
reqPost.write(dataString);
reqPost.end();
reqPost.on('error', function(e){
console.error(e);
});
}
//Could build feature inputs from web form or RDMS. This is the new data that needs to be passed to the web service.
function buildFeatureInput(){
console.log('===performRequest()===');
var data = {
"Inputs": {
"input1": {
"ColumnNames": ["gl10", "roc20", "uo", "ppo", "ppos", "macd", "macds", "sstok", "sstod", "pmo", "pmos", "wmpr"],
"Values": [ [ "0", "-1.3351", "50.2268", "-0.2693", "-0.2831", "-5.5310", "-5.8120", "61.9220", "45.3998", "-0.0653", "-0.0659", "-30.3005" ], ]
},
},
"GlobalParameters": {}
}
getPred(data);
}
function send404Reponse(response) {
response.writeHead(404, {"Context-Type": "text/plain"});
response.write("Error 404: Page not Found!");
response.end();
}
function onRequest(request, response) {
if(request.method == 'GET' && request.url == '/' ){
response.writeHead(200, {"Context-Type": "text/plain"});
fs.createReadStream("./index.html").pipe(response);
}else {
send404Reponse(response);
}
}
http.createServer(onRequest).listen(8050);
console.log("Server is now running on port 8050");
buildFeatureInput();
But can i do this by using axios call or express server.
can anyone help me with proper syntax if i can do this using either vue axios or express server.
It sounds like you want to use express in server with axios in Vue front page instead of Node http server with https client in server-side.
To replace Node http with express is very easy, it is as below.
const express = require('express')
const path = require('path');
const app = express()
const port = 8050
app.use(express.static(path.join(__dirname, '.')))
app.get('/', (req, res) => res.sendFile('index.html'))
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err)
})
app.use(function (err, req, res, next) {
if(err.status == 404) {
res.status(404).send("Error 404: Page not Found!")
}
res.status(500).send("Error 500: Internal Error!")
})
app.listen(port, () => console.log("Server is now running on port 8050"))
But consider for the security of the api-key value for calling Azure Machine Learning Studio API, I recommended not to call the API with axios in Vue front page and still make the calling works in the server-side by express, as below.
const axios = require('axios');
var host = 'ussouthcentral.services.azureml.net'
var path = '/workspaces/fda91d2e52b74ee2ae68b1aac4dba8b9/services/1b2f5e6f99574756a8fde751def19a0a/execute?api-version=2.0&details=true'
var api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=='
const pred = axios.create({
baseURL: 'https://'+host,
timeout: 1000,
headers: {'Content-Type':'application/json', 'Authorization':'Bearer ' + api_key}
});
app.post('/mls-api', (req, res) => pred.post(path, JSON.stringify(req.body)).then(function(resp) {
resp.pipe(res)
}))
Then, you can call /mls-api url from Vue front page with the data value below.
var data = {
"Inputs": {
"input1": {
"ColumnNames": ["gl10", "roc20", "uo", "ppo", "ppos", "macd", "macds", "sstok", "sstod", "pmo", "pmos", "wmpr"],
"Values": [ [ "0", "-1.3351", "50.2268", "-0.2693", "-0.2831", "-5.5310", "-5.8120", "61.9220", "45.3998", "-0.0653", "-0.0659", "-30.3005" ], ]
},
},
"GlobalParameters": {}
}
axios.post('/mls-api', data)
.then(function (response) {
console.log(response);
})
I have this code in Ionic app but I don't know how to make an API with Node.js to send this values to sever only by using Node.js.
submitForm() {
let headers = new Headers(
{
'Content-Type': 'application/json',
'Accept': 'application/json'
});
let options = new RequestOptions({ headers: headers });
let data = JSON.stringify({
Value1: this.form.value1,
Value2: this.form.value2,
Value3: this.form.value3
});
console.log(data);
let url = 'http://localhost:3000/calculate';
console.log(url);
return new Promise((resolve, reject) => {
this.http.post(url, data, options)
.toPromise()
.then((response) => {
console.log('API Response : ', response.status);
resolve(response.json());
})
.catch((error) => {
console.error('API Error : ', error.status);
console.error('API Error : ', JSON.stringify(error));
reject(error.json());
});
});
}
You may like to use ExpressJS. Following example may help you
Create a directory lets called api with following 2 files
create app.js in api directory
var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/calculate', function(req, res) {
var data = req.body;
console.log('Here are your data: ', data);
res.json({message: 'you posted successfully'});
});
var port = process.env.PORT || 3000;
var server = http.createServer(app);
server.listen(port);
server.on('error', function(){
console.error('Error')
});
server.on('listening', function(){
console.log('server started on port ' + port)
});
create package.json file in api directory
{
"name": "api",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"body-parser": "~1.17.1",
"express": "~4.15.2"
}
}
now open command line/terminal and install dependencies by running following command(you must go to inside api directory)
npm install
now you can run by just running either npm start or node app.js
You should google for learning and studying and post questions for bug/issue
Update: without any dependencies or library but not recommended
It will be better to use http framework like express, sailsjs, etc. but if you like to play with nodejs then following example may help you
var http = require('http');
var port = process.env.PORT || 3000;
var server = http.createServer(function(req, res) {
var contentType = req.headers['content-type'];
var rawData = '';
req.on('data', function (chunk) {
rawData += chunk;
});
req.on('end', function () {
if(req.method === 'POST' && req.url === '/calculate' && contentType.indexOf('application/json')>-1){
try {
const data = JSON.parse(rawData);
console.log('Your data is here: ', data);
res.writeHead(200, { 'Content-Type': 'application/json'});
var result = {message: 'you have posted successfully'}
res.end(JSON.stringify(result));
} catch (e) {
console.error(e.message);
res.writeHead(400, { 'Content-Type': 'application/json'});
var result = {message: e.message}
res.end(JSON.stringify(result));
}
} else {
res.writeHead(404, { 'Content-Type': 'application/json'});
var result = {message: 'Url not found'}
res.end(JSON.stringify(result));
}
});
});
server.listen(port);
server.on('error', function(){
console.error('Error')
});
server.on('listening', function(){
console.log('server started on port ' + port)
});
I've created an example Node.js project that illustrates client/server request/response using AJAX and JSON. It "requires" only 'http', 'path', and 'fs'.
It implements a 'calculate' function server-side, and has a web page that presents 3 input boxes and a 'Calculate' button.
It's on Github: "https://github.com/bobwirka/NodeClientServer.git"
Hope this will be of help.
I want to render to the ui / print to console log some object value from GET response.
I'm using Node JS for my server side and HTML + JS for my client side.
Because my goal is to render data and the request type is cross domain I can't use "fetch" function.
My only alternative to execute it is to send it by "JSONP" dataType.
Actually, the request is sent and the response receives by callback as well, but my code is print "null" to the console and not the response data.
When I've tried to used JSON.parse() it received a "parseerror".
The expected result it's to get only the image tag value (2.0.90) and to print this inside the console log / render it to the UI.
async function uiChecking() {
let index;
const hostsDock = [qa + dockers];
let lengthVal = hostsDock.length;
for (let hostIndxD = 0; hostIndxD < lengthVal; hostIndxD++) {
index = hostIndxD;
let url = hostsDock[hostIndxD];
$.ajax({
url: url,
dataType: 'jsonp',
}).done( function(data) {
console.log("A " + data);
});
}
}
**Server.js **
var express = require('express');
var cors = require('cors');
var app = express();
var path = require("path");
var fetch = require('fetch-cookie')(require('node-fetch'));
var btoa = require('btoa');
var http = require('http');
var corsOptionsDelegate = function (req, callback) {
var corsOptions;
if (whitelist.indexOf(req.header('Origin')) !== -1) {
corsOptions = { origin: true } // reflect (enable) the requested origin in the CORS response
}else{
corsOptions = { origin: false } // disable CORS for this request
}
callback(null, data , corsOptions) // callback expects two parameters: error and options
};
app.engine('.html', require('ejs').__express);
app.set('views', __dirname + '/view');
app.set('view engine', 'html');
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res){
res.render('index');
res.render('logo');
res.writeHead(200, {'Content-Type': 'application/json'});
});
// app.get('/products/:id', cors(corsOptionsDelegate), function (req, res, next) {
// res.json({msg: 'This is CORS-enabled for a whitelisted domain.'})
// });
app.get('/data/:id', function (req, res, next) {
var opts = {
host: config.alertService.host,
port: config.alertService.port,
method: 'GET',
path: '/DataService/rest/receiveData/' + req.params.id
}
var reqGet = http.request(opts, function (dataResponse) {
var responseString = '';
dataResponse.on('data', function (data) {
responseString += data;
});
var response = {x:[],y:[],z:[],t:[]};
dataResponse.on('end', function () {
var responseObject = JSON.parse(responseString);
var accs = responseObject.data.listPCS;
for(var i in accs){
response.x.push(accs[i].accX);
response.z.push(accs[i].accY);
response.y.push(accs[i].accZ);
response.t.push(accs[i].timestamp);
}
res.jsonp(response);
});
});
reqGet.end();
reqGet.on('error', function (e) {
console.error(e);
});
});
if (app.settings.env === 'production') {
app.error(function(err, req, res) {
res.render('new404.html', {
status: 500,
locals: {
error: error
}
});
});
}
app.listen(8033, function () {
console.log('CORS-enabled web server listening on port 8033')
});
You need to iterate through the response to return the result e.g..
$.each(data, function(index) {
console.log(data[index].ui);
console.log(data[index].id); console.log(data[index].Name);
});