Running Node.js server on windows - node.js

How can I host node.js application on windows server?

I suggest you use iisnode to achieve your requirement.
You should firslty install the IIS URL Rewrite extension, node.js, iisnode.
After you installed above things, you could find you IIS Modules contains the IISnode feature, then you could run your node.js application on IIS as other web application.
More details about how to host node.js application, you could refer to below article.
https://www.hanselman.com/blog/InstallingAndRunningNodejsApplicationsWithinIISOnWindowsAreYouMad.aspx
https://www.simplymigrate.com/2017/04/11/internet-information-server-iis-node-js-in-producton-iisnode/

1) install nodejs (Download from here)
2) write your server program (it should contain a proper node.js listener code)
3) run your code; open a Powershell or CMD and type the following command:
node my_server.js
you can also refer to these links:
Install Node.js and NPM on Windows
PM2 | process manager for Node.js
P.S:
Here is a very very simple node.js server code (from node.js docs here):
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Hope it helps!

Related

How to deploy the node.js application on server with different node version

I want to deploy my node app on server. but when I upload the node app to our server and then add node.js instalation then It is not opening the file. the index.js file is
const connectToDb = require("./DBconfig");
const express = require('express')
const router = express.Router();
var cors = require('cors');
connectToDb();
const app = express();
const port = process.env.PORT | 5000;
app.use(cors());
app.use(express.json());
connectToDb();
app.use( '/api/user' ,require('./routes/user') );
app.use( '/api/post' ,require('./routes/post') );
app.get('/', (req, res) => {
res.send('The site is start now')
})
if(process.env.NODE_ENV === "production"){
app.use(express.static("client/build"));
const path = require("path");
app.get("*",(req,res)=>{
res.sendFile(path.resolve(__dirname,'client','build','index.html'))
})
}
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
The node version in my local machine is v16.14.0 but the node version that the NameCheap is given is v14.18.3
the node installation in Namecheap that I configure is the installation of node
the error message is triggered, when I hit the URL of API API's URL, is the error message. I am unable to find the solution to solve this problem. I read some articles in which the writer tells that this type of error occurs when we use the different node versions in the local machine and on the server machine but I can't do anything because I restrict to use of the v14.18.3 on the server-side but I used the v16.14.0 in my local machine. can anyone tell me the solution to solve this type of problem?
webi is also a great tool. I recommend completely uninstalling node and installing it fresh with webi.
# this will install node and the webi cli
# Linux/Mac
curl -sS https://webinstall.dev/node | bash
# Windows
curl.exe -A "MS" https://webinstall.dev/node | powershell
# then you can run the following to switch versions
webi node#14
See https://webinstall.dev/node/ for more info.
nvm is another useful tool that can help you to control the node version itself, you can use nvm commands to upgrade the node version quite easily.
nvm install <version-number>
nvm use 0.10
using nvm use -nodeversion you can switch your node js version to your server node js version.

BASH: express command not recognized

I've read other forums on installing express, I have run
npm install express -g on command prompt as admin.
I did this command a few times and restarted my computer multiple times but Express is still not recognized.
Any help is appreciated.
Express isn't a web server; it is a framework for building web servers.
The express package doesn't provide anything directly executable.
The express documentation has a getting started guide.
If you aren't trying to do server-side programming using Node.js and just want an HTTP server, then the http-server package may be more your speed.
Assuming you’ve already installed Node.js, create a directory to hold your application and make that your working directory.
then follow the below command
npm init
follow step press enter and after
npm install express
make index.js file paste below hello work program.
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}`)
})
Now you can run file using below command
node index.js
Open your broswer and enter URL : http://localhost:3000/
You can display Hello World!
You have successfully installed node js and express js program.

Browser downloads the file instead of showing html

I have developed MERN app in my local system. I am trying to host it AWS-EC2 (Free Tier).
Never thought deploying would be so painful.(Well, i am beginner to Node. Earlier i have worked with php. I found it easier to integrate)
I referred this article to install Node and Express. Node is successfully installed. I created sample file 'test.js' with following code:
var http = require('http');
var port = 9000;
http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'test/plain'});
res.end('Hello world!\n');
}).listen(port);
console.log('Listening on port',port);
After executing
node test.js
the browser downloads a file with no extension. Opened in editor and it has 'Hello World' in it. Nothing else. I am pretty sure i have followed all steps properly. But still i ended up 'Downloading the file'
Can anyone help me with this issue?
BONUS QUESTION : How do i deploy MERN app in AWS EC2?
Have a nice day
Thanks
try this (you forgot to add the listen callback, and a typo in content type):
var http = require('http');
var port = 9000;
http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'text/plain'});
res.end('Hello world!\n');
}).listen(port, err => {
if (err) throw err
console.log('Listening on port',port);
})
Use appropriate content type for your html page.
res.writeHead(200,{'Content-Type':'text/html'});
For a viewable html code, text/html should be set as content type instead of test/plain.

Cannot access Nodejs/Express Server on macOS High Sierra from other computer

I started an simple express server on MacOS High Sierra. localhost:3000 and 127.0.0.1:3000 work fine. However, when I accessed the server via http://192.168.x.x:3000 from another computer, I didn't get any response (ERR_EMPTY_RESPONSE).
I tested the server on another MacBook (MacOS Sierra) and a Windows computer, everything works fine.
So I suppose there are something wrong with MacOS High Sierra.
Any help would be welcome. Thanks in advance.
Here is the code :
const express = require('express')
const app = express()
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(3000, () => console.log('Example app listening on port 3000!'))
UPDATE MacOS
I have tried to reinstall node, npm, change share settings, but they don't work, neither.
Finally, I updated MacOS (10.13.4 --> 10.13.5) and now other computers can access my server like a charm.
----------Update -------------
After a few (happy) day, the issue raised again.
Fortunately , now I pretty sure that ESET EndPoint Security was the problem since it's the last application I installed.
So I open the ESET firewall and create a new rule which allow local network address to connect via TCP&UDP.
The server once again works like charm.
Hope this help the other.
Refer to https://nodejs.org/en/docs/guides/getting-started-guide/
Once you have installed Node, let's try building our first web server. Create a file named "app.js", and paste the following code:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
After that, run your web server using node app.js, visit http://localhost:3000, and you will see a message 'Hello World'

Howto run google assistand bitcoinInfo example webhook index.js

I am trying to run the google assistant example webhook nodejs application(index.js) on my own server, but don't know what is the webhosting setup for this..
https://codelabs.developers.google.com/codelabs/your-first-action-on-google-with-webhook/#2
What is the environment to run this app on my server? Since it's not running as a listening server, I can't use nginx, node_cgi is not mature with apache, how am I supposed to run this sample?
Excellent point, and you should be sure to file a bug request on the page to indicate it is unclear.
The code, as presented, is meant to run using Google Cloud Functions.
This doesn't mean you can't run it on your own server - just that you need to know how to run a Node.js server outside of your Apache or Nginx environment. I've seen a number of configurations, but typically you'll have the Node.js server application running and listening to a local port and have a proxy between your externally facing web server at a particular path and this port.
But even that isn't sufficient in this case - the code itself doesn't listen on a port - it expects to be handed a request and response object in the form that Express.js with a JSON middleware can handle. To do that, you'll need to have installed the Express.js library and then start listening with code such as:
const express = require('express');
const app = express();
app.use( express.json() );
app.get('/', (req, res) => exports.bitcoinInfo( req, res ));
app.listen(3000, () => console.log('App listening on port 3000!'));
Thanks for the help to #Prisoner and #Ido Green link works even better! The minimum to run the sample I did the following:
Create a new nodejs project with mainfile main.js, install express and actions-on-google
mkdir googleActionServer
cd googleActionServer
npm init
npm install --save actions-on-google
npm install --save express`
Copy the index.js from google and put this into main.js
const express = require('express');
const bitcoinInfo = require("./index");
const app = express();
app.use( express.json() );
app.post('/', (req, res) => bitcoinInfo.bitcoinInfo( req, res ));
app.listen(3000, () => console.log('App listening on port 3000!'));
Start the application by running:
node ./main.js
To test with DialogFlow, download and install ngrok to /usr/local/bin for ex and then run:
ngrok http 3000
Ngrok will give you an url that is accesible from outside and forward the requests to the nodejs app. It will also create a https for you, so copy paste the https address into DialogFlow webhook address and you are set to go

Resources