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

in #utopian-io6 years ago (edited)

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

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

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

image.png

and choose your own operating system.

Creating the folder

image.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

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

image.png

image.png

image.png

change the index.js to server.js.

image.png

image.png

image.png

image.png

put your name or leave it blank

image.png

press enter here >
image.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

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,

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);

});

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

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

image.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

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

as you can see, it's work perfectly!

What we learn next time

  • Chat system based on socket.io



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

I tried following your tutorial but get this far and it no longer works for me in windows....

npm.png

How do I proceed to the next step?

Your contribution cannot be approved because it does not follow the Utopian 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.
[utopian-moderator]

Okay, thanks.
hope next tutorial (chat system with socket.io) will be accepted.

I hope too.
Good continuation

Coin Marketplace

STEEM 0.20
TRX 0.12
JST 0.028
BTC 65809.08
ETH 3604.05
USDT 1.00
SBD 2.54