Nodemon not reloading page - javascript

I am starting studying Node.js, so I am using nodemon to reload my page, but it's not working and already tried all Stack solutions aswell.
Look how simple is my code:
package.json
{
"name": "api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.4"
},
"devDependencies": {
"nodemon": "^1.18.3"
}
}
server.js
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hey');
});
app.listen(3001);
My bash while interacting with res.send() message.

You are using nodemon with the server. It is restarting the server as you make changes to the server.js file. That is, your endpoints are being updated. This will not cause your client to reload. As you are simply navigating to the endpoint that you are creating within the browser, you will not see the changes reflected without refreshing.
That isn't to say that there is no benefit to running nodemon in this way. If you were not doing so you would need to also close the node instance (ctrl-c) and then rerun it every time before refreshing the page. Otherwise, you would still be running the old version of your server and still see the same content served.
Eventually you will consume these endpoints using an http client from your client application, this is generally when you take advantage of a hot reloading environment. There are some options here if you want to make express live-reload before then.

Related

Websocket connection to 'wss://url' failed : [vite] server connnection lost. polling for restart... after deploying the vitejs project in AWS EC2

I have created the vite project it is working perfectly fine in the localhost. But when my project was deployed in AWS EC2 instance, I am getting following error message :
Websocket connection to 'wss://www.domain.com/3000' failed:
[vite] server connnection lost. polling for restart...
pacakge.json :
{
"name": "screen-recording-demo",
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"serve": "vite preview"
},
"devDependencies": {
"vite": "^2.0.5"
},
"dependencies": {
"firebase": "^8.2.10"
}
}
I have used firebase dependencies in my project in locally everything is working fine. I have checked the vite doumentation, but not getting what needs to be added or I am missing in the production.
based on my experience this a day ago, i need to open the port used
in Nuxt 3 it's 24678, so i must execute these
ufw allow 24678/tcp

Node.js - How to run multiple servers using package.json

Trying to run multiple separate servers in parallel from package.json. This method only runs server1.js, but ignores server2.js
Package.json
{
"name": "demo",
"version": "1.0.0",
"description": "demo",
"main": "server1.js",
"dependencies": {
"express": "^4.14.0",
"socket.io": "^1.5.1"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server1.js && node server2.js",
}
}
P.S -- code will be used in AWS
In the shell, && is a command that executes the next command after the previous command is executed, and if the state is true.
If you want multiple servers to run in parallel at the same time, use the background process commands, & or nohub.
node server1.js & node server2.js &
and now, your application runs in the background, and the process information can be obtained with the command below.
If you want to stop this, get pid information with netstat command
You must delete it with the command kill -9 <your_procees_id>.
netstat -vanp --tcp | grep <your_server_port>
kill -i <your_process_id>
Alternatively, there may be a monitoring module such as pm2.
https://www.npmjs.com/package/pm2

Nodemon not refreshing browser in React-Express-Node App

I have installed nodemon locally in my workspace, but even though it restarts in the terminal after changes are made, it does not refresh the browser page. I have to manually refresh it each time.
I've got Express, Node, React and Webpack running in the environment.
This is how my setup looks like -
My package.json starts up server.js -
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
},
and server.js is -
var express = require('express');
var app = express();
app.use(express.static('public'));
app.listen(3000, function () {
console.log("Express server is up on port 3000");
});
The entry point in the webpack config file is -
module.exports = {
entry: './public/scripts/app.jsx',
output: {
path: __dirname,
filename: './public/scripts/bundle.js'
}
What should I do to fix it?
Update -
I made a video to describe the situation, if it helps.
nodemon is only for restarting the server when your server code changes. It has no functionality to reload your page in the browser. If you want automatic browser reload, you could, for example, run a webpack dev server in addition to your nodemon. webpack dev server is able reload the page in the browser when your client code changes, it can even update the page in the browser without a full page reload, if you use its hot module reloading feature.
in package.json
"scripts": {
"start": "nodemon server.js -e html,js,css"
},
in server js
var reload = require('reload')
app.listen(3000, () => {
console.log(`Listening on port 3000`);
})
reload(app);
in index.html
<body>
<h1>ron</h1>
<script src="/reload/reload.js"></script> <!-- it's necessary -->
</body>
For those needing to use nodemon and reload browser on change as well, I replied here https://stackoverflow.com/a/51089425/7779953
Solution is Nodemon + Browser Sync + Gulp + Express server

How to run Express on io.js

So Node.js was forked late last year and the forked version is io.js.
I can't find anything of a setup guide on the docs. I'm fairly new, anyone know how I can setup io.js using Express web framework? Thanks!
Do the steps in answer 1, then create an index.js file like this
var express = require('express');
var app = express();
app.use('/resources', express.static(__dirname + '/resources'));
app.get('*', function (req, res, next) {
res.send('hello world');
res.end();
});
app.listen(3000);
and a package.json file like this
{
"name": "iojsexpress",
"version": "0.0.0",
"description": "Get express working with iojs",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.10.7"
}
}
and then run the following commands
npm install
iojs index.js
and visit localhost port 3000 in your browser and you should see "hello world"
Actually io.js wasn't released yet. First release will be at January 13th(or 14rth) (see here). At this time the best you can do to setup io.js is to clone its repository:
git clone https://github.com/iojs/io.js
and try to build it manually. On Unix/Max it looks like:
./configure
make
make install
But I do not recommend you to do this. Beware: now very active preparation for the first release is going on. Lots of commits with possibly breaking changes. So better wait less then one week until first official io.js version will be released.

Failed to load resource socket.io in nodejitsu?

I'm trying to deploy a simple chat applicacion in Nodejitsu, and using socket.io.
I use the application localy and it works perfectly but when I deploy to nodejitsu I get the following error in the javascript console:
Failed to load resource
And the chat doesnt work.
I checked to the source code and clicked in the
/socket.io/socket.io.js
and it works perfectly
My package.json is:
{
"name": "Chat_1",
"subdomain": "Chat_1",
"scripts": {
"start": "node main.js"
},
"version": "0.0.0-4",
"engines": {
"node": "0.10.x"
},
"dependencies": {
"socket.io": "*"
}
}
I hope someone could help me, please!
According to the Nodejitsu Handbook "We reroute all traffic to port 80". Double check you're using port 80.
server.listen(80);

Categories

Resources