I have 2 qeustions :
1. I want my project's url to be like 127.0.0.1:8080/param=id and I couldnt do it, I tried:
app.get('/param=id', function(req, res) {
console.log(req.param("id"));
});
if I write '/param/:id' it works but I dont want the url to look like this
I want my program to send message according to the id a json message or string to the client
So my second question is how the client gets the response - I want the message to go throgh a script in the client's side?
I would suggest using req.query instead of req.params:
app.get('/', function(req, res) {
console.log(req.query.id);
// or you may still use req.param("id")
});
requesting it like
HTTP GET 127.0.0.1:8080/?id=my_id
query is a different way of sending data to the server, designed to send key-value pairs.
Though, if id is the only thing you want to send to the server, I would recommend to stick with params, e.g.:
app.get('/:id', function(req, res) {
console.log(req.params.id);
});
requesting it like
HTTP GET 127.0.0.1:8080/my_id
Related
I'm attempting to make a request to an API to get a users' avatar. This is the code I have, however it's returning a "player is not defined" error.
app.get(`/api/avatar/${player}`, function(req, res) {
res.redirect(`http://cdn.simulping.com/v1/users/${player}/avatar`);
});
Essentially, if the URL is /api/avatar/3925 it would send them to http://cdn.simulping.com/v1/users/3925/avatar.
Assuming you're using Express.JS for the routing, the correct way for doing that is:
app.get('/api/avatar/:id', function(req, res) {
res.redirect(`http://cdn.simulping.com/v1/users/${req.params.id}/avatar`);
})
Assuming you are using Express routing, then you would need to utilise route parameters.
The Express documentation provides:
Route parameters are named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the req.params object, with the name of the route parameter specified in the path as their respective keys.
In this case:
Route path: /api/avatar/:player
Request URL: http://localhost:3000/api/avatar/3925
req.params: { "player": "3925" }
The working code:
app.get('/api/avatar/:player', function(req, res) {
res.redirect(`http://cdn.simulping.com/v1/users/${req.params.player}/avatar`);
});
You can read more about it in the Route Parameters section of the Express Routing Guide.
I'm setting up routes for in my express server. As I tried to use it with a route paramter, I stumbled upon a problem:
router.get('/edit/:id', (req, res) => {
res.sendFile(rootPublic + '/pages/recipes/edit.html')
})
Is it possible to somehow send the data of the object I want to edit with the response? Or is it only possible to use the id as a query parameter and read the data on document load?
I have a post method to grab some data from a form. I then have to render another form which has a different post route. I want to use the variable I got from the first post method in the new post method.
I have a post method to grab the variable username
router.post('/', function(req, res, next) {
var username = req.body.username
res.render('add_user', {username: username})
});
Now I want to use it in a different post method:
router.post('/add_user', function(req, res) {
// I want to use the username variable here
})
Note: both the post methods are in the same file named index.js
Any help would be appreciated.
There are a few ways to accomplish this. First way would be for the second request to pass in the username.
But if you need to maintain a variable on the server and use it between requests, just put it outside of the handlers. Something like this:
let globalUsername= "";
router.post('/', function(req, res, next) {
var username = req.body.username;
globalUsername = username;
res.render('add_user', {username: username})
});
router.post('/add_user', function(req, res) {
res.send(globalUsername);
})
Not a very good solution though; unless you will have only 1 user hitting your website. If you have multiple users, then you need a way to identify them. Common ways include the client passing the username (or a token representing the user) on every request, or a cookie, etc. Look into sessions, cookies, JWTs, and other auth mechanisms.
But if you just want to share a variable across a couple of different requests, just put the variable on the global scope (or anywhere where both handlers can read it.)
So I have an API that works fine and responds as I need, but I was wondering how I would set the response from the API to be in the body?
Right now it's just setting the response to be in a <pre> tag on the actual website (if I inspect element it shows like below):
<body><pre>{api response}</pre></body
but the only way I can access it is if it's in the body response.
If I check the console.log one the response, it just shows a bunch of blank spaces, so I was wondering how I would set up the API so it will respond in the body? Any help would be appreciated.
For the API I'm using Node.js with express and request packages if that helps at all
I'm not sure if i understand your question, but i'm sure that you can insert your response in any place you want. Try using jade or mustache (handlebars).
Here is the example from the Express website:
app.get('/', function (req, res) {
res.send('Hello World!')
})
This sends "Hello World!" in the HTTP response body.
So to send the API result instead you need to:
Set the appropriate content-type header
Send the result of getting stuff from the API
Such:
app.get('/', function (req, res) {
var api_response = get_data_from_api();
res.set('Content-Type', 'application/xml'); // or application/json or whatever
res.send(api_response);
})
I have a server with Node.js and I use Express to build a web app.
My server is already able to get an array from a database using a function (rss_interrogDB). Now I want to use this array to display a list in the html page. But I must be missing something...
On the server-side my code is:
app.get('/', function(req, res) {
rss_interrogDB(function() {
// don't konw what to add here
});
On the html page the code is:
$.get('/', {}, function(data){
// not sure it is the right code
console.log(data);
// then the code to create a list from the array (no pb with that)
});
Thank you for your help!
Assuming your DB gives you an array of data, add something like this to your server code:
app.get('/', function(req, res) {
rss_interrogDB(function(serverData) {
res.send(serverData);
});
});
You could manipulate the serverData object and build one that is more suitable for the client before you send it back, but that depends on what you want to do.