node index.js command start and end server in the same time - javascript

I did install WSL2 and ubuntu 22.04 distribution on windows 11. I configured envoirment, installed nvm, node v.16.17.1, next in my folder did npm init, installed express, created index.js file within simple structure:
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
When i tap node index.js nothing happen.
image
In my second computer with ubuntu 20.04 distribution everything work fine. Does anyone know what to do?

How about doing debugging by referring to https://expressjs.com/en/guide/debugging.html?
like this,
DEBUG=express:* node index.js
If no other problem is found, it would be good to use the --inspect options.
https://nodejs.org/en/docs/guides/debugging-geting-started/

Problem resolve. File need to be SAVE before calling from terminal....

Related

Static files not showing on express server

I am currently trying to get my JS express server to display my static HTML files using this code.
My JS Express code
**When I run my server I get this error message Error: ENOENT: no such file or directory, stat: and the directory path to the file. the directory path exists so I don't understand why it wont show the file and its contents. could someone explain this to me please. **
What I have tried in addition to my written code.
**I have tried changing the location of my project entirely and that didn't work.
I have tried using app.use, __Fileneme instead of __dirname, rewriting the code nothing works.
Im using Node. js I have express installed and required I have rewritten the code several times to be sure the error was not syntax error "although VS code would have made me aware of any syntax errors" I just at a loss now. this is a simple server that should display my html files just fine yet nothing I do works.**
try adding this line at the top:
// where /public is the folder where 'welcome.html' is
app.use('/public', express.static(__dirname+'/../public'));
the npm path package may be helpful as well
app.get('/Home', (req, res) => {
console.log(path.join(__dirname ,'/public/welcome.html'));
res.sendFile(path.join(__dirname ,'/public/welcome.html'));
})
const express = require('express');
const app = express();
app.use(express.static('public',{
extensions: ['html', 'htm']
}));
// Listen to the App Engine-specified port, or 8080 otherwise
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}...`);
});

Restart express server every time a request is made to an endpoint

basically I would like to 'restart' it every time a client sends a request to /reset, how can I do that? any help is extremely valuable, I don't know how to approach this yet
As #jfriend00 mentioned, You can use pm2 like applications to monitor the process. Below is simple steps that can be followed.
Node Js code: Exit the process on route
const express = require("express");
const app = express();
app.get("/", (_, res) => res.send("hello"));
app.get("/restart", (_, res) => {
process.exit(0);
});
app.listen(8080, () => console.log("Server is running on :8080"))
Run server using pm2 in watch mode:
./node_modules/.bin/pm2 start app.js --watch
He also mentioned that you should not use this in the production environment. I support that statement. You should not use this approach in prod environment.

React & Node JS deployment on Apache server

I am trying to deploy my react app with node js as a backend on apache server on a shared hosting. My folder structure is lke following:
client //frontend folder along with build folder inside it "created
by npm run build"
config
controllers
models
node_modules
routes
package.json
package.lock.json
server.js
I have checked numerous tutorials and articles on the internet but failed to deploy it. Do I need to create ".htaccess" file? Can anyone guide me how to deploy it properly.
My server.js code:
// ROUTES
app.use("/api/sms", sms);
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "/client", "build", "index.html"));
});
//CONFIGURE PORT
const PORT = process.env.port || 5000;
const server = app.listen(PORT, () =>
console.log(
`Server running in "${process.env.NODE_ENV}" mode on port "${PORT}"`
)
);

Make node.js server listen on Heroku

I'm building a simple app that connects to a Node.js server.
Now, I've successfully tested the server locally: I run "node server.js" on a cmd window, the server's listening, and then I can use whichever routes I'd like.
However, using "http://localhost:8080/" obviously isn't going to work on my phone, and therefore I've deployed my code to Heroku. I can run this file there, and get the reply it sends, but my question is - how do I make it continuously listen?
I tried searching for an answer online, but no luck..
here's my server.js file:
// dependencies
var express = require('express');
var app = express();
var port = Number(process.env.PORT || 8080);
var routes = require('./routes/api');
var bodyParser = require('body-parser');
//express
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//routes
app.use('/api', routes);
app.get('/', function(req, res) {
res.send('Our first route is working! :)');
});
app.get('/test', function(req, res) {
res.send('Testing successful!! :)');
});
//start the server
app.listen(port, function() {
console.log('Listening on port ' + port);
});
Not sure what do you mean by "continuously listen". Heroku builds the project and runs when you deploy it. All you need to do is create a heroku project and push your code to instance. Monitor the logs on heroku so that you know that your project is successfully deployed and there is no error. You will have to install heroku toolbelt to execute heroku related commands. All the details for the deploying node application to heroku are provided here
To send your server to heroku, follow the steps provided in this link https://devcenter.heroku.com/articles/git
Hope that helps
The call to
app.listen(port, ...
does continuously listen.
Assuming your deploy worked, go to your Heroku app's url ( e.g. http://cookienator.herokuapp.com/api/test) and notice it's listening.
app.listen(process.env.PORT,process.env.IP);
This may work.
It didnt work on running port argument for me.
The above code worked for me.
Also dont forget to add app.js or start page in package.json

Run Node.js on a Google Compute Engine Debian server

I have a debian server running on Google Compute Engine with a host like example.com and I'm trying to run a node.js app on a directory like example.com/mynodeapp.
Node.js, NPM are installed properly on the debian server.
I'm running pm2 start main.js from the root of example.com/mynodeapp and everything running but when I go to example.com/mynodeapp, I have nothing, just the indexing of the files.
Express.js configure
main.js (entry)
var express = require('express')
var vhost = require('vhost')
express()
.use(vhost('example.com/mynodeapp', require('./dist/index').app))
.listen(8080)
dist/index.js
var express = require('express')
var app = express()
app.get('/', function(req, res) {
res.send('Hello World!');
})
exports.app = app
With .listen(8080) the port is set to 8080, so you'll have to change that or try example.com:8080.
Note that you will run into one of two problems, depending on your choice: Port 8080 is probably not open – you'd have to allow it in the firewall.
If you're currently getting a file listing on port 80, there's some other server running (possibly apache or nginx from the standard debian install). You will have to stop that server to free up the port.

Categories