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
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
if you're not using windows go to this download link - https://nodejs.org/en/download/
and choose your own operating system.
Creating the folder
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.
(do not use names with spaces, if it is with space change it like I did)
change the index.js to server.js.
put your name or leave it blank
press enter here >
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".
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"
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
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.
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
I tried following your tutorial but get this far and it no longer works for me in windows....
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
Thanks :)