What is sense of NodeJS modules without any exports? - node.js

If there's any. Im not really into web technologies, but have to understand some awful code written by someone else in Node.

All node.js apps are npm modules once you execute npm init. After that point, you can publish the module by executing npm publish assuming you gave it a unique name in the package.json.
If the app isn't meant to return anything, then there is no need to export anything. However, it's almost always worth exporting something to allow for unit testing deeper than just starting the app as an http server and sending it requests.
It is also sometimes useful to modify the way your app runs based on whether it is being required as a module or executed as an app. For example, say i have an express rest api server. I can run it as a standalone server on api.example.com, and then require it into another application and run it from that application directly to avoid CORS issues without having to duplicate code or deal with git submodules, instead I simply npm install the api into the application that needs it and attach it just like you would a router. www.example.com/api
app.use('/api', require('#myusername/api.example.com'))

Related

Can I run a front-end and back-end on Netlify?

I want to practice creating my own RESTful API service to go along with a client-side application that I've created. My plan is to use Node and Express to create a server. On my local machine, I know how to set up a local server, but I would like to be able to host my application (client and server) online as part of my portfolio.
The data that my client application would send to the server would not be significant in size, so there wouldn't be a need for a database. It would be sufficient to just have my server save received data dynamically in an array, and I wouldn't care about having that data persist if the user exits the webpage.
Is it possible to use a service like Netlify in order to host both a client and server for my purposes? I'm picturing something similar to how I can start up a local dev server on my computer so that the front-end can interface with it. Except now I want everything hosted online for others to view. I plan to create the Express server in the same repo as the front-end code.
No, Netlify doesn't allow you to run a server or backend. However, they do allow you to run serverless functions in the cloud. These can run for up to 10 sec. at a time. Furthermore Netlify also have a BETA solution called "background functions" That can run for up to 15 minutes. But honestly for a RESTful API there sure would be better solutions out there?
If you are still looking for the Netlify for Backend you can consider Qovery. They explained here why it is a good fit for their users.

Does express generate default request logs inside a cloud run container? Should I keep them on in my production environment?

I have an express server inside a Cloud Run Docker container.
I'm getting those logs:
Are those generated by the express package? Or are those logs somehow generated by cloud run?
Here is what express docs says: https://expressjs.com/en/guide/debugging.html
Express uses the debug module internally to log information about route matches, middleware functions that are in use, application mode, and the flow of the request-response cycle.
But it does not give much detail on what those logs are and how you enable or disable them.
Should I leave them on? Won't it hurt my server's performance if it's going to log every request like that? This is NODE_ENV === "production.
These logs entry are generated by the Cloud Run runtime platform. It's not your Express server. The performance aren't impacted by this log, and in any cases, you can't deactivate them.
You could exclude them to save space in logging capacity (and save money), but I don't recommend this. They brings 3 of 4 golden signals of your application (error rate, latency, traffic). Very important for production monitoring

Is there a way to serve mock data to a Vue.js application with the dev server?

I'm working on a Vue.js application that retrieves some data with ajax calls; in the dev environment I'm playing with the application in the browser using the builtin server and mocking the external resources with an API stubber that runs on its own server. Since both pieces of software are using node I'm wondering if there's a way to run both with a single command, serving the Vue.js application code and some static files for the mocked calls (which are not all GETs and require parameters, so sticking the json files in the app public folder wouldn't work).
Edit: let me try to clarify. The Vue.js application will interact with a backend service that is still being developed. On my workstation I can play with the application by just running the npm run serve command, but since there's no backend service I won't be able to try the most interesting bits. Right now I'm running saray alongside my application, serving some static json files that mock the server responses. It works fine but I'm effectively running two separate http servers, and since I'm new to the whole Vue, npm & javascript ecosystem, I was wondering if there's a better way, for instance serving the mock responses from the same (dev) server that's serving the Vue application.

Node.js server automatically start

I'm new to Node.js but willing to give it a serious try. Coming from PHP things seem a bit confusing as there is no index.php, but a start-script needs to be executed to fire up the server npm start.
How is this done in production? Are there pre-run scripts? What if the server closes for some reason, how do I get it back up automatically without having connection problems for the clients? Will it automatically work for the domain, or does it also mean someone alwasy has to go to domain.com:3000?
Ar am I thinking about this the wrong way?
What you are asking is very broad in term of question . Let me give the idea how it work.
Coming from PHP things seem a bit confusing as there is no
index.php, but a start-script needs to be executed to fire up the
server npm start.
So in node.js we have a file by which we start our node server and that we decide what we want . mostly people use app.js , server.js , index.js
when you run npm start , that means you would have package.json in the folder that file have written start: node app.js . And when you run npm start , it get fire .
How is this done in production? Are there pre-run scripts?
NODE_ENV=production npm start , you can access this in node code like this
process.env.NODE_ENV . in this way you can add dev,qa tag for each environment .
I will recommend you to have a look in
http://pm2.keymetrics.io/
What if the server closes for some reason, how do I get it back up automatically without having connection problems for the clients?
For that reason you can look at https://nodejs.org/api/cluster.html
You can manage the crash thread and then open another thread as node is single thread.
Also you can manage the node.js all type of error By this . This make node.js catch all exception and error
https://nodejs.org/api/process.html
process.on
does it also mean someone alwasy has to go to domain.com:3000?
No. You can take any port you want . 80,8080 whatever . I will recommend to use nginx in front of node.js application . But for less compatibility go for simple node application .
for example:-
var http = require('http');
var port = 3000 ; // take any 80, 8080
http.createServer(function (request, response) {
response.writeHead(200, {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin' : '*'
});
response.end('Hello World\n');
}).listen(port);
Hope this help .
Answering you 1st question there are other options how you can run node application. I suggest that you start using some packages likeNodemon
which are actually built for this purpose.
Answering your second question, you can use same for production deployments using some container system if you like. here are some optionsdocker, kubarnetees,and many more.
Your automatic restart thing can be solve either by you container manager or package you have used for deployment.
And for redirecting all request coming on 80 or 443 port that you want to redirect to your application you can try nginx.
There are some modules which you can use to restart the server automatically if it closes for some reason. Some of them are pm2, forever.
Without going into much detail, you have to be VERY clear of the following:
Your node web process will die. Yes, that's right, when there is an uncaught exception it can die. Therefore, you need more than one process for failover, and for that there are many techniques and libraries. Some of them are:
Several node processes load balanced behind a web server (nginx most commonly used)
Managed cluster of node processes (https://github.com/Unitech/pm2)
Or (not that good for production in my opinion) some process monitor which will restart your node web process if it dies:
https://github.com/foreverjs/forever

Requests to api on Heroku-hosted NodeJS app not making it to app

I'm hosting a NodeJS+Express+Mongoose app on Heroku and have been struggling to understand why some of my GET requests are serving stale data, after repeated changes to resources using the api. The changes only get reflected through the GET call after about half an hour or if I restart the server.
The stale resource data gets returned for requests from multiple clients, my Angular JS webapp as well as curl. So, I'm guessing it isn't something to do with caching on the client.
Based on the logging it looks like the requests aren't even making it to the Express app routes, so I'm guessing there's some kind of caching that's happening on the server. I don't see this behaviour on my local system, so is it something that can be disabled on Heroku?
Generally, if you are using Express then your request passes through multiple Express Middleware before actually reaching your app routes. You have to see if there is some error in any of those middleware or there is some validation error for the data which you feed in.
I would suggest you to exhaustively test the data which failed on the heroku app on your local app too.

Resources