Nodejs Simple Website

Posted on

If Needed install Nodejs and npm by running the command

sudo apt-get install nodejs npm

Installation of each can be verified by typing

node -v
npm -v

Node installation can also be tested by typing

node
>3+1
4

Install the npm package called http-server. The http-server package was created by the amazing IndexZero (aka Charlie Robbins). Navigate to directory

sudo npm install -g http-server

Change the directory to the /home/pi/Public directory to store and serve up public web files. Create file index.html and and insert test html.

cd /home/pi/Public
sudo touch index.html
sudo nano index.html

<html>
<head>
<title>Welcome</title>
</head>
<body>
Welcome to my website!
</body>
</html>

Save and close index.html, verify the file exist in the directory then launch http-server

ls
#Verify that the file exists
index.html
#Start Nodejs Web Server
http-server

Starting up http-server, serving ./
Available on:
http://127.0.0.1:8080
http://192.168.0.230:8080
Hit CTRL-C to stop the server

open webrowser and enter the IP address or http://localhost:8080 which should open website displaying “Welcome to my website!”

On a different computer on the same network the same webpage should be accessible by typing the http://[IP address]:8080 or http://[hostname]:8080