Is all communication from frontend to backend done via routes? - node.js

I'm working on a vuejs/express fullstack web app, and I know you can specify endpoints on the server like:
app.get('/', function (req, res) {
res.send('GET request to the homepage')
})
Which you can then hit from the client to display the home page.
But I'm wondering what about when you don't need to go to a 'route'? For example, you just want to send some data from client to server to do some calculations and then send data back to the client - do you still specify an endpoint e.g /FunctionName and access it from the frontend in the same way or is there some other way to call a function in the backend?

This is the 'express' way to define endpoints (or routes), regardless if it will return an html page like the example you've specified, or do some computation by calling other functions with user-specified parameters.
As a basic example:
app.post('/myendpoint', function (req, res) {
returnValues = callMyFunction(req)
res.send(returnValues)
})

Related

Restrict access to nodejs express routes

I have "internal" routes in my app :
app.get('/myroute', function(req, res) {
res.status(200).send('Hello!');
});
But the route is also accessible from outside:
curl http://example.com/myroute/?this=that
How to restrict the use of this route to the app / domain itself, ideally with a nice error?
As I see it, you have a few options:
Consider whether this route needs to be a web route at all, and not a function. If it's internal, surely a function would suffice?
Create a 'shared secret' for this function that must appear in either the header or as a request param. Then only callers that know this secret can call it.

Node and Express – Conditionally Displaying Pages

What I want to do seems elementary; but, I am running into some blocks.
All I want to do is display pages based on a condition.
var express = require("express");
var app = express();
app.get('/', function(req, res) {
if (userIsLoggedIn()) {
res.sendFile(__dirname + '/public/index.html');
} else {
res.sendFile(__dirname + '/public/accessDenied.html');
}
});
I am looking to grab information from the browser – I want to call a function from another browserify-ed file, and use the return value to determine which page is displayed to the user.
I can't run the server from app.js because it needs to be browserify-ed since it requires Web3. And since the function relies on state, I am not sure how to access this state from the server file.
You have to post data from the browser to the server either with a form or query parameter.
For example:
web3.shh.post(object [, callback])
https://web3js.readthedocs.io/en/1.0/web3-shh.html#post
on the server site you need to extract those values and have to reply based on your posting.
How to process POST data in Node.js?
Unfortunately your use case is not clear ,but in general you might want to check out how to handle HTTP API communication.

Nodejs express: how to use common function needed by different routes logic

How can I add common methods/functions that can be used by different routes built using express in nodejs?
I created a nodejs server that is currently serving 3 APIs. For example:
/cars
/cars/:id
/cars/:id/sellers
All these 3 APIs retrieves data from another external APIs. So, I already have some code that is similar in all of those routes. For example, sending a GET request to the external API endpoint but with different parameters.
For the last API in my list above, I want to enhance it so that it will take optional paramters (lastname, firstname). I could have another API endpoint like this:
/cars/:id/sellers/:lastname&:firstname
But in this case the logic for this and the 3rd API in my list will be almost identical.
I'd like to create some util functions that I can call from any of my API routes with different parameters. I'm not quite sure to do this or where to begin.
Any suggestions?
app.use((req, res, next) => {
//req.someParam
//your logic
next();
});
You can use a middleware like this make sure to add it after your routes has defined, it will work as a common middleware in the routes
router.post('/cars', (req, res, next) => {
// if() some condition
req.someParam = "param";
next();
});

ExpressJS / NodeJS routing traffic to the base URL instead of root?

I have a route defined as:
router.get('/console/:labID?', (req, res, next) => {
When this route gets called, all additional requests are appended to /console to give you a request similar to :
/console/<new request>
I'm assuming this is intended functionality and I need to reset the route somewhere based on the request? Occasionally I will want to append it but where do I control this behavior?

Socket.io and request and response objects

I'm quite new on node.js and now I'm learning socket.io.
I'm developing an app step by step so, at this time, I have an app that can login an user, do crud operation in mysql and mongodb and upload files, all these operations are manage with some web pages with HTML and javascript technologies launched directly from restify.
After that I'm tring to add socket functionality to, at this time, simple print who is online.
So, before I have something like:
server.get('/login', function(req, res, next){ ... });
and now I have something like:
socket.on("login", function (req, res, next){ ... });
but, naturally, req and res are undefined!
Are there the same objects into socket.io?
To my understanding, you want to pass values back and forth in your request and response using socket.io.
Yes it is possible to do that and you syntax should be something like this...
Using express.js:
io.on('login', function(req){
client.emit('response event', { some: 'data' });
Note: when using emit you send the data to everyone, you have other methods like .broadcast(), .to(), etc.. for other use cases refer to socket.io github for better understanding
And lastly, inside emit you define the function you want to call on the client side and the data you want to send to the client.

Resources