How are HTTP responses managed by Node JS express? - javascript

Having this simple express app example:
var express = require('express')
var app = express()
app.use(express.static(__dirname+'/public'))
app.get('/', function(request,response) {
response.send("This wont matter if we got an index.hml after all")
})
app.listen(2311, function() {
console.log("app escuchando en Maricela DDMM")
})
And at /public I got an index.html.
When I get rid of such html the string in the send() method will be sent, received and rendered at browser.
What does actually happen with the response.send() string talking about the HTTP response, as the HTML is which is send and so rendered at browser?

Express goes through the chain of middleware in the order in which it was added. You have added express.static as the first middleware, so it will be ran first.
In the event express.static cannot find a file, it calls next(), allowing the next bit of middleware to run. This is your handler set up with app.get('/' //..., which sends the data as you have told it to.

I think it basically sets up the header information based on the parameter in send and then send the http response

Related

Is there a way to serve files to browser and data to rest requests using the same endpoint?

I've followed this 7 hour tutorial on creating a blog almost to. There is one step that I skipped, because I did not want to use the separate client/server hosting that the tutorial suggests. Instead, I have a single heroku server that serves the client out of the public server:
const app = express();
app.use(express.static('public'))
app.use('/posts', postRoutes);
app.use('/user', userRoutes)
You can see that the app also serves some rest requests to the /posts and /user paths. However, the tutorial also led me to add these paths into the url client-side.
For example, you can access my app at (https://blog-tutorial-888.herokuapp.com), but you will be immediately "redirected" to (https://blog-tutorial-888.herokuapp.com/posts).
I say "redirected" because on the client side, it appears that you are at that site, and the purpose is so that you can do things like navigate to the next page, which will be at (https://blog-tutorial-888.herokuapp.com/posts?page=2).
But if you were to actually go to these links, or refresh the page, you will be hit with the result of the rest request, which is a giant block of text (and this is obviously because I have app.use('/posts', postRoutes)).
Is there a way to get around this somehow? Somehow serve both the html and the rest request at this endpoint?
To have a single server that severs the front and data trough a REST API, you would need to differentiate the paths, for example by adding an /api to urls for getting data, like so:
app.use('/api/posts', postRoutes);
app.use('/api/user', userRoutes);
And then below all your /api handlers add following lines so that for every other request you send that HTML that would load React bundle:
app.get("/*", (req, res) => {
// Make sure it's the correct path to the build folder
res.sendFile(path.join(__dirname, "../client/build/index.html"));
});
Of course don't forgot to add /api in your front end query urls as well, not the routes setup:
fetch("/api/posts")
// or
axios.get("/api/posts")

node js express js request response circle

I am curious and really willing to learn and understand the whole concept behind the request and response circle of all backend
my question is I have a node js express framework
app is started
app.use('/api',router)
require('./routers/user')(router)
require('./routers/uretim')(router)
require('./routers/durus')(router)
require('./routers/rapor')(router)```
all my functions are called and executed and waiting for a request
since the order of this app is first use
app.use('/api',router)
then the router is called at this particular point router function has nothing attached to it,
so does it mean as we started our application we have created the routes with the executed functions below the app.use
main question
user enter ..../api
our back end was hit
what is the first process, or function that happens within our backend?
So the USE is used to load middleware, what you have is to use the router middleware on the /get url..
A better example of what is happening here is to define some action middleware on that url :
app.use("/api", (req, res, next) => {
// For example, a GET request to `/api` will print "GET /api"
console.log(`${req.method} ${req.url}`);
next();
});
When you start your server, if you hit your /api url, it will log the request info in the console and continue.
However the other URL's will not.. So you can tailor specific middleware for certain endpoints.
As for your routing itself, you will need to define the routes and their controller logic. Something like :
// HTTP GET Request Definition
app.get('/api',(req, res) => {
res.send("Hey, you hit API");
} );
This will happen after the middleware has been actioned.
Express

console.log() in client from express server

Is it possible to use console.log() in the backend (I am using express) to output things in the frontend.
for example:
const express = require('express');
const app = express();
client.console.log('Hi');
How would I do it?
Thanks.
There is no built-in support for a server to cause something to show in the client console. I'm not sure what the actual use case for that is since the console is typically a debugging aid, not an actual end-user thing.
In any case, if you want to do that, you would have to have cooperating code on both the client and the server and then how that code works depends upon the context in which you want to put the info in the console.
From a page load
From a page load, the server could embed a small script in the page that would output into the browser console when the page loads and runs.
From an Ajax call
Here, you could include a property in some returned JSON that contains the desired console message and then the client code making the ajax call would have to grab that property and call console.log() with it.
From any random time on the server
If you're not in the context of an existing request from the browser or web page Javascript (as in the previous two points), then you would need some push channel connected between the web page and the server such as a webSocket connection, a socket.io connection or a SSE connection. Then, you could send a message to the client and the client would need some code listening for those incoming messages and then display them in the local console upon receiving them.
try it:
The 'send' method of 'res' object of Express, is one of so many ways to send a response to client in the request event.
const express = require('express')
const app = express()
app.get('/test', (req, res) => {
return res.send('Hello world!')
})
There is no support as jfriend00 said, but there is a way to go around this.
So lets say the user requests /test
You want to displayin his console Hello World
So you do:
const express = require('express')
const app = express()
app.get('/test', (req, res) => {
return res.send('<script>console.log(Hello world!)</script>')
})
And that acts as a full client console log. There ya go. (Single Time)
Or setup socket.io as jfriend said in his post if you want to constantly post console messages (multiple times).

Running a NodeJS cron from cron-job.org

I am trying to run a NodeJS cron at a set interval using cron-job.org, but they don't have any documentation about how to actually run the script.
Basically the service visits a URL that you provide at a set interval, but I am specifically confused about what kind of code I can put on the endpoint (specifically what type of code will actually run). Can someone provide an example of what I would put at the endpoint URL?
You can do something really simple using either the HTTP module in Node.js or the popular Express module. Using express you can do something really simple like:
var express = require('express');
var app = express();
app.get("/test", function(req, res, next){
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ status: 'OK', timeStamp: new Date().toISOString() }));
});
console.log('Express listening.. on 3000');
app.listen(3000);
You can really run anything you like in the /test endpoint, though when it's being called from cron-job.org they'll probably stop if you keep throwing back 400 errors at them or the script takes really long to execute.
You'd call this using the url
http://yourdomain:3000/test
And of course you might well want to change the port number and path!
cron-job.org only allows you to call an endpoint at a set time interval.
If you want to have some code run at a set interval without worrying about HTTP server, deploying, etc... Check out services like chunk.run
Here's an example code:
https://chunk.run/c/scheduled-run-example
Then you can just select the trigger "Scheduled" like so:

Node Js server refreshing page when receiving a JSON update

My application is a web interface to monitor changes on "objects" processed by the pc and in particular when they get over a particular threshold.
The Node Js server runs on the same pc and has the only use of displaying data on a table, refreshing it when one of them reached a given threshold.
At that point the program that calculates the "objects" opens a socket to the Node Js server and sends the json data.
My issue is to reload the page on the user browser to diplay the new resoults without having the user to manually hit the browser refresh button.
For the server I used the express, express-ejs-layouts and body-parser modules.
This is my server code:
// require our dependencies
var express = require('express');
var expressLayouts = require('express-ejs-layouts');
var bodyParser = require('body-parser');
var app = express();
var port = 3000;
// use ejs and express layouts
app.set('view engine', 'ejs');
app.use(expressLayouts);
// use body parser
app.use(bodyParser.json({extended : true}));
// route our app
var router = require('./app/routes');
app.use('/', router);
// set static files (css and images, etc) location
app.use(express.static(__dirname + '/public'));
// start the server
app.listen(port, function() {
console.log('app started');
});
Leaving aside page estetics (css,images, layouts) the core of the server,
serving GET and POST requests is on the routes.js file that, on GET requests renders the pages passing the JSON data to the javascript page containing the table and on POST requests saves the JSON passed by my "objects" calculator.
This is the routes.js code:
// require express
var express = require('express');
var path = require('path');
// create our router object
var router = express.Router();
// export our router
module.exports = router;
router.post("/", function(request, response) {
global.exportage = request.body;
});
// route for our homepage
router.get('/displayobj', function(req, res) {
//res.send(global.exportage);
res.render('pages/displayobj',{data:global.exportage});
});
How you can see I'm using a quite horrible global variable to pass data but I wanted to keep it as simple as possible.
How could I force the reload on the user browser when a new JSON is received?
I tried using location.reload(true) but I get an error during execution saying: "location is not defined".
The problem, in my opinion, is that the server satisfies the GET request issued by the browser and nothing else happens cause the communication is already completed. I would like not to refresh the page with a fixed interval of time but using the new JSON as triggering event (edited after reading comments).
You cannot do real time communication from server to client with simple HTTP requests. Consider using either long polling or websockets.
The simplest solution for you is to use a library like Socket.io that handles it. When the content need to be refreshed, send an event to the client and then either refresh using window.location.reload() or update the content with the DOM API.

Categories