create account

NodeJS Tutorials #1 - Creating a static site [Node.JS] by lonelywolf

View this thread on: hive.blogpeakd.comecency.com
· @lonelywolf · (edited)
$0.39
NodeJS Tutorials #1 - Creating a static site [Node.JS]
#### What Will I Learn?

- How to create simple NodeJS server
- How to make a static site
- Running the server

#### Requirements

- No special requirements
- NodeJS installed on your system - [Download Here](https://nodejs.org/en/)

#### Difficulty

- Basic

#### Tutorial Contents
This tutorial shows you how to run a NodeJS server and how to build it,
it's easy and basic tutorial and this is gonna be a series of tutorials.

## Installing Node.js
First of all, we need to install Node.js,

go to the download link - https://nodejs.org/en/

and install the stable version

![image.png](https://cdn.utopian.io/posts/3f0dea2c51f21e4d6a68dd8434e99ea13d04image.png)

if you're not using windows go to this download link - https://nodejs.org/en/download/

![image.png](https://cdn.utopian.io/posts/bb27a2d20e770bac72d1a0cffcdb6fe22cc5image.png)

and choose your own operating system.

## Creating the folder
![image.png](https://cdn.utopian.io/posts/0ed4b235537ff89d5bdc1c59fc1a9ac8b67eimage.png)

press SHIFT + right click and open PowerShell window, then type

> npm init

then follow the screenshots (I'm using GIT Bash, it will be the same with PowerShell)

press enter only if I didn't tell other.

![image.png](https://cdn.utopian.io/posts/9ff3ed949f81e52cf63a2da98d58919a439cimage.png)

(do not use names with spaces, if it is with space change it like I did)

![image.png](https://cdn.utopian.io/posts/b2c1966593d73849d638c9c83d158222c729image.png)

![image.png](https://cdn.utopian.io/posts/3982ceb2101b71d4f46ec3dfcb1b3e5201ebimage.png)

![image.png](https://cdn.utopian.io/posts/cacd6a799d4b15afe58aaea538885ea455ddimage.png)

change the index.js to server.js.

![image.png](https://cdn.utopian.io/posts/475f7405fb8b1713f043d399f97231b55b58image.png)

![image.png](https://cdn.utopian.io/posts/a512aef7c395a70c2fafe83a9e0df08f0aabimage.png)

![image.png](https://cdn.utopian.io/posts/caa756d4d14ad6073b625007eae6ec870c2aimage.png)

![image.png](https://cdn.utopian.io/posts/504c7a167d5d529f7592b65034ab583f3196image.png)

put your name or leave it blank

![image.png](https://cdn.utopian.io/posts/a95a1f96970164a2b9b682584e91f56072b1image.png)

press enter here >
![image.png](https://cdn.utopian.io/posts/3e7a74321f86467582474ad051bd80c57116image.png)

great! we done with the package.json.

## Creating basic server and run it
go to your text editor, make a new js file in your folder.

the file name needs to be the same as you put on the command line.

if you didn't change the name to server.js(like I said to) the name will be "index.js".

![image.png](https://cdn.utopian.io/posts/9cbabbd5e225f69f77e3ace586978a3250b2image.png)

Good, now we have a blank page, let's make a server!

> const http = require('http');

first, we need to set a variable with the "HTTP"(which create the server).

> const hostIP = "localhost"; //or "127.0.0.1" which is better for you.

> const port = 80; //you can use any other port, like 3000 or something.

now make 2 variables, the IP, and the port, you want to make variables so you can change it anytime you want to.

now let's create a simple server,

 <blockquote>
const server = http.createServer((req, res) => {

  res.statusCode = 200;

  res.setHeader('Content-type', 'text-plain');

  res.end('Hello World! this is my first project at node.js!');


});

server.listen(port, hostIP, () => {

  console.log("Listen to port " + port);

});

</blockquote>


pastebin version(full script) -> https://pastebin.com/ex5AfL46 OR [Here](https://pastebin.com/ex5AfL46)

when you done, save the script and creating a "server.bat"

![image.png](https://cdn.utopian.io/posts/6feac5526338df81ca4f693992925191994dimage.png)

and just put this command

> node server

if you followed all the steps it should work!
let's start the "server.bat" if you're not using windows use the command line and type the same command,

> node server

then you should get this window

![image.png](https://cdn.utopian.io/posts/4cd270912cea2e45f746d3ac31c63f900a51image.png)

if you done it correctly go to your web browser and go to "localhost:80" if you're using port 80 you can just go "localhost", if you're using any other port use "localhost:PORT" and change "PORT" to your port.

![image.png](https://cdn.utopian.io/posts/d1598582d1456bdae37103b2ca3b207389b6image.png)

as you can see, it's work perfectly!

## What we learn next time

- Chat system based on socket.io

<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@lonelywolf/nodejs-tutorials-1-creating-a-static-site-node-js">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , , , , , , ,
properties (23)
authorlonelywolf
permlinknodejs-tutorials-1-creating-a-static-site-node-js
categoryutopian-io
json_metadata{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":27193779,"name":"node","full_name":"nodejs/node","html_url":"https://github.com/nodejs/node","fork":false,"owner":{"login":"nodejs"}},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","nodejs","node","tutorial","server"],"links":["https://nodejs.org/en/","https://nodejs.org/en/download/","https://pastebin.com/ex5AfL46","https://utopian.io/utopian-io/@lonelywolf/nodejs-tutorials-1-creating-a-static-site-node-js"],"image":["https://cdn.utopian.io/posts/3f0dea2c51f21e4d6a68dd8434e99ea13d04image.png","https://cdn.utopian.io/posts/bb27a2d20e770bac72d1a0cffcdb6fe22cc5image.png","https://cdn.utopian.io/posts/0ed4b235537ff89d5bdc1c59fc1a9ac8b67eimage.png","https://cdn.utopian.io/posts/9ff3ed949f81e52cf63a2da98d58919a439cimage.png","https://cdn.utopian.io/posts/b2c1966593d73849d638c9c83d158222c729image.png","https://cdn.utopian.io/posts/3982ceb2101b71d4f46ec3dfcb1b3e5201ebimage.png","https://cdn.utopian.io/posts/cacd6a799d4b15afe58aaea538885ea455ddimage.png","https://cdn.utopian.io/posts/475f7405fb8b1713f043d399f97231b55b58image.png","https://cdn.utopian.io/posts/a512aef7c395a70c2fafe83a9e0df08f0aabimage.png","https://cdn.utopian.io/posts/caa756d4d14ad6073b625007eae6ec870c2aimage.png","https://cdn.utopian.io/posts/504c7a167d5d529f7592b65034ab583f3196image.png","https://cdn.utopian.io/posts/a95a1f96970164a2b9b682584e91f56072b1image.png","https://cdn.utopian.io/posts/3e7a74321f86467582474ad051bd80c57116image.png","https://cdn.utopian.io/posts/9cbabbd5e225f69f77e3ace586978a3250b2image.png","https://cdn.utopian.io/posts/6feac5526338df81ca4f693992925191994dimage.png","https://cdn.utopian.io/posts/4cd270912cea2e45f746d3ac31c63f900a51image.png","https://cdn.utopian.io/posts/d1598582d1456bdae37103b2ca3b207389b6image.png"],"moderator":{"account":"zonguin","time":"2018-03-29T16:49:25.064Z","flagged":true,"reviewed":false,"pending":false},"questions":[],"score":null}
created2018-03-27 18:33:24
last_update2018-03-29 16:49:24
depth0
children5
last_payout2018-04-03 18:33:24
cashout_time1969-12-31 23:59:59
total_payout_value0.310 HBD
curator_payout_value0.075 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,589
author_reputation25,295,791,457,391
root_title"NodeJS Tutorials #1 - Creating a static site [Node.JS]"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id46,948,441
net_rshares194,814,370,579
author_curate_reward""
vote details (11)
@flxlove ·
$0.04
I tried following your tutorial but get this far and it no longer works for me in windows....

![npm.png](https://cdn.steemitimages.com/DQmewZ4wG188DtHdGibY4rur4bRdPaKLkgZPLJqVRcsKrRR/npm.png)

How do I proceed to the next step?
👍  , , , , , , ,
properties (23)
authorflxlove
permlinkre-lonelywolf-nodejs-tutorials-1-creating-a-static-site-node-js-20180612t133652080z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"image":["https://cdn.steemitimages.com/DQmewZ4wG188DtHdGibY4rur4bRdPaKLkgZPLJqVRcsKrRR/npm.png"],"app":"steemit/0.1"}
created2018-06-12 13:36:48
last_update2018-06-12 13:36:48
depth1
children0
last_payout2018-06-19 13:36:48
cashout_time1969-12-31 23:59:59
total_payout_value0.034 HBD
curator_payout_value0.004 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length228
author_reputation3,476,351,344,093
root_title"NodeJS Tutorials #1 - Creating a static site [Node.JS]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id60,447,006
net_rshares15,176,852,904
author_curate_reward""
vote details (8)
@zonguin ·
Your contribution cannot be approved because it does not follow the [Utopian Rules](https://utopian.io/rules).

I cannot accept your contribution because they are basic instructions except for the part "creation of the server". But this last one lacks details and explanations

*Submissions presenting content creation and simple on-screen instruction will be rejected.*

You can contact us on [Discord](https://discord.gg/uTyJkNm).
**[[utopian-moderator]](https://utopian.io/moderators)**
properties (22)
authorzonguin
permlinkre-lonelywolf-nodejs-tutorials-1-creating-a-static-site-node-js-20180329t165209054z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-03-29 16:52:12
last_update2018-03-29 16:52:12
depth1
children3
last_payout2018-04-05 16:52:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length489
author_reputation12,831,796,837,639
root_title"NodeJS Tutorials #1 - Creating a static site [Node.JS]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id47,309,665
net_rshares0
@lonelywolf ·
Okay, thanks.
hope next tutorial (chat system with socket.io) will be accepted.
properties (22)
authorlonelywolf
permlinkre-zonguin-re-lonelywolf-nodejs-tutorials-1-creating-a-static-site-node-js-20180329t165304718z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-03-29 16:53:24
last_update2018-03-29 16:53:24
depth2
children2
last_payout2018-04-05 16:53:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length79
author_reputation25,295,791,457,391
root_title"NodeJS Tutorials #1 - Creating a static site [Node.JS]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id47,309,835
net_rshares0
@zonguin ·
I hope too.
Good continuation
properties (22)
authorzonguin
permlinkre-lonelywolf-re-zonguin-re-lonelywolf-nodejs-tutorials-1-creating-a-static-site-node-js-20180329t165358702z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-03-29 16:54:00
last_update2018-03-29 16:54:00
depth3
children1
last_payout2018-04-05 16:54:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length29
author_reputation12,831,796,837,639
root_title"NodeJS Tutorials #1 - Creating a static site [Node.JS]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id47,309,928
net_rshares0