javascript - How to execute a Node.js script on the server? - Stack Overflow

I'm beginning to learn web technologies and programming and I'm setting up my own local webse

I'm beginning to learn web technologies and programming and I'm setting up my own local webserver. I have HTTPD, PHP, Python, MySQL all up and running on Windows. Now I want to add Node.js to the mix. I installed the Windows 64 bit installer. Now how do I begin? I have a basic Hello World script in a test.js file. But when I access this file in the browser it only displays as plain text. It's not executed. How to execute a Node.js script on the server?

I'm beginning to learn web technologies and programming and I'm setting up my own local webserver. I have HTTPD, PHP, Python, MySQL all up and running on Windows. Now I want to add Node.js to the mix. I installed the Windows 64 bit installer. Now how do I begin? I have a basic Hello World script in a test.js file. But when I access this file in the browser it only displays as plain text. It's not executed. How to execute a Node.js script on the server?

Share Improve this question asked Feb 7, 2017 at 14:39 AardoAardo 2271 gold badge4 silver badges11 bronze badges 7
  • node /path/to/your/file.js – Aer0 Commented Feb 7, 2017 at 14:41
  • Sorry for my newbie. Ok. It works. But how do I deploy it to a Web Hosting server online? I don't suppose I log into the server and type 'node file.js' every time I want the server to run – Aardo Commented Feb 7, 2017 at 14:53
  • Currently you've to type in that mand. Manually or through a service/tool/whatever like Jenkins. There are a lot of SaaS provider where you can automatically deploy and start your services in a GUI. Take a tour through Google. – Aer0 Commented Feb 7, 2017 at 14:58
  • I see. One more question. How about the port? I expect users to open my URL without specifing a designated port. Just www.domain. i.e. How to run Node.js on a default port? – Aardo Commented Feb 7, 2017 at 15:02
  • @Aardenon check my answer – Ahmed farag mostafa Commented Feb 7, 2017 at 15:04
 |  Show 2 more ments

3 Answers 3

Reset to default 3

Starting a node script is pretty simple. Just use your mand line or terminal and execute the following mand.

node /path/to/your/file.js

Doing so you'll start your node script. If you're going to start a server it's pretty much the same. Just keep in mind to define a server in your node file and start it. A simple server using express could look like this (You can also use a fully node way, this is just a simple example using express. You may check google for how to set up a simple node http server).

var express = require('express');
var app = express();
var port = 4000;

app.listen(process.env.port || port);

As you can see the specified port is set to 4000. You can simply adjust this by changing the value itself or passing in a node environment variable. To pass in an environment variable just start your server like this.

node port=3000 /path/to/your/file.js

This will finally pass the value of port to process.env.port which obviously will start your server on the port 3000.

you can use these packages to keep the file running so you won't have to login to server every time :-

forever and you can just write :-

forever start app.js

nodemon

nodemon app.js

pm2 which is very useful , as it will auto restart your app when it crash or any error happens

pm2 start app.js

Your run your file: node server.js

Then it starts.

There should be specified in code on which port you run your server. Then it is accessible for example at http://localhost:3000/


As Quentin noted, I was thinking about "creating web server". Of course, you can run javascript code with Node.js without server. Then skip the part abot localhost, just use node test.js in console.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744176095a4561762.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信