I made a small server. When I am trying to make a get request to my server in browser i can see correct data, but when I am trying to make a post request to my server by code I get httpstatus - 404. Why can it happened?
My server code:
var express = require('express');
var http = require('http');
var app = express();
app.get('/api/check', function(req, res) {
res.send('{"debug": "on"}');
});
exports.app = functions.https.onRequest(app);
You are not accepting POST requests to your API right now, only GET. To accept POST requests, add this:
app.post("/api/example", function(req, res) {
// do your logic here
}
Because your server doesn't have a POST route request handler. So it shows 404 Not Found Message. Add a POST route same as GET
Related
I am creating rest web service using express nodejs framework. I want to navigate to second.js page from first.js and need to send data and header to it.
first.js
const express = require("express");
const http = require("http");
var router = express.Router();
var options = {
host: "localhost",
port: "3000",
path: "/second",
method: "POST"
};
router.get("/", function(req, res, next) {
http.request(options);
});
module.exports = router;
second.js
var express = require("express");
var router = express.Router();
router.get("/", function(req, res, next) {
res.send("Hello");
});
module.exports = router;
app.js
app.use("/first", first);
app.use("/second", second);
I tried like above but it doesn't navigate to second.js web service. Does anyone know what I am doing wrong ?
Your code is not performing a redirection. You are actually making a call from within the first endpoint to a second endpoint and acting more like a proxy.
A redirection is performed by calling res.redirect as defined in the express documentation at http://expressjs.com/en/api.html (Search for res.redirect). A redirection returns a HTTP redirect response that the users browser will follow to a new URL. Unfortunately, redirections do not allow headers to be passed and it will be executed as a GET call(This is what redirections are designed for.). You can include whatever query params you want in the URL though and set cookies (refer to How to forward headers on HTTP redirect)
I think you are using POST in your request
But only set up GET on your second endpoint
I have the following code:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/rasp', function(req, res) {
res.send("received");
res.send(req.body.data);
});
app.listen(process.env.PORT || 5000);
I used POSTMAN to see if it worked and apparently the "received" text is sent back, but the data parameter is blank. What could be the problem?
Basically, the client sends a request and waits for a single response from your server. Once the client receives that response, it stops waiting for another. Furthermore, Express only allows you to send one response per request (going along with the client stuff explained above). You may be able to change this setting, but I've never dealt with it, so my answer will be limited to that knowledge.
Your server is executing res.send('received'); and the response is handled. You cannot call res.send again. You should be getting an error on your server when you attempt the second call.
You should send all data that the client needs in the first (and only) res.send().
Server responses should not be handled like logging (ex: sending 'received', 'analyzing', etc). Keep the logging separate. The client doesn't want to know all that extra info, it just wants the expected data response.
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.listen(process.env.PORT || 5000);
app.post('/rasp', function(req, res) {
res.send({received:true,data:req.body});
});
can you try this one and writing the response here
I believe your post body is "data=Some Value".
If you want to send multiple chunks of data, you should use res.write, and res.end. In your code change the following lines
res.send("received");
res.send(req.body.data);
to
res.write("received");
res.end(req.body.data);
So the client makes a get request using a button on the index page. This sends some information to a route which has been set up as follows:
app.js
var route = require('./routes/index');
var button = require('./routes/button');
app.use('/', index);
app.use('/button', button);
The request is sent from a client-side directory to the node framework whenever someone presses the button. If the request is sent to 'localhost:port/button', then the button.js file mentioned above will receive the request. So in the button.js file we have something like the following:
var express = require('express');
var router = express.Router();
var someData = '';
router.get('/', function (req, res, next) {
//make a get request here such that someData
//receives whatever the request returns from another
//set up framework(i.e. Spring...)
someData = getRequest('some other URL');
res.send(someData);
};
module.exports = router;
The problem here is that the getRequest('some other URL') within the router get request never receives any information.
Also (as a side-note), I cannot seem to find in the express API as to why we have
router.get('/')...
instead of
router.get('/button')...
to access and make requests to the button page.
Any help would be very much appreciated!
You want to make a request to some other REST API running somewhere else right?
You can use node-rest-client for that.
I guess you are confusing what is server code and client code or do I´m missing something?
I´ll try to explain the way it works:
Server code:
var express = require('express');
var router = express.Router();
var someData = '';
router.get('/yearStations/:id', function (req, res, next) {
//make a get request here such that someData
//receives whatever -the id parameter- the request returns from another
//This is express.js
someData = getYourDataFromDataBase(req.params.id);
res.send(someData);
};
module.exports = router;
Client code:
JS (angular.js)
$scope.getInfo = function() { //here you make the GET call to the server
$http.get("http://localhost:XXXX/yearStations/Spring").then(
function(success){
alert(success.data);//this should be your data
});
};
HTML
<button ng-click="getInfo()">getInfo</button>
I am working on node.js project which uses express framework .
My application will process bunch of POST requests . One of my post request is follows
URL
POST /processit
request params
info={"one":"a=5"}
node.js code
var express = require('express');
var app = express();
app.use(express.bodyParser());
app.post('/processit', function(req, res) {
console.log(req.body);
res.type('text/plain');
res.send('Testing !');
});
app.listen(process.env.PORT || 3000);
In node.js log i am getting respone
{}
But if i change request params from info={"one":"a=5"} to info={"one":"ab5"} i am getting
info={"one":"ab5"}
in node.js log .
I don't know whether i did anything wrong here
ScreenShot :
Thanks in advance .
I suspect your issue is with how your Eclipse test tool is encoding things.
Try doing your POST with curl from the command line, or this Chrome plugin: https://chrome.google.com/webstore/detail/rest-console/cokgbflfommojglbmbpenpphppikmonn?hl=en
I'm trying to do a ajax post to a heroku server app and have the app return a response to the client web page (on a different server).
It works just fine if I test it locally (using localhost) but when I push to heroku, I get no response.
app.js:
var express = require('express');
var app = express();
app.use(express.bodyParser()).post('/', function(req, res){
res.header("Access-Control-Allow-Origin", "*").header("Access-Control-Allow-Headers", "X-Requested-With").header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
res.end("test");
});
port = process.env.PORT || 3000;
app.listen(port);
client-side js:
$.post("http://myapp-name-here.heroku.com", {query: "anything", val: "something"},
function(data){
console.log(data);
});
Edited in:
When I open the client-side html page, when it makes the post request, it gets the status "canceled" after a couple of seconds. Any ideas?
myapp-name-here.heroku.com is no longer supported, try myapp-name-here.herokuapp.com