Skip to content

Commit 5867b5d

Browse files
authored
Update README.md
1 parent 41e7628 commit 5867b5d

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

README.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,70 @@
11
# nodejs-website
2-
This is a sample basic Node.js website using plain HTML files and the built in http module.
2+
3+
This is a sample basic Node.js website using plain HTML files and the built in http and fs modules.
4+
5+
This tutorial will walk you through the creation of a very basic website using only Node.js built in modules. Follow these steps to create a Node.js website:
6+
7+
## Steps
8+
9+
1. Create a folder for your Node.js project.
10+
2. Create a home page HTML and call it index.html. Add the required HTML tags and some basic content in the index.html file.
11+
3. Create a second file and for now, call it another-page.html. Add the required HTML tags and some basic content in the another-page.html file.
12+
4. Add a link in the index.html page to antoher-page.html. And add a link in the another-page.html to index.html. This will allow for easy testing of pages.
13+
5. Create a new file called app.js.
14+
6. In app.js import the required modules:
15+
16+
```
17+
var http = require('http');
18+
var url = require('url');
19+
var fs = require('fs');
20+
```
21+
22+
7. Create an http server:
23+
24+
```
25+
http.createServer(function (req, res) {
26+
}).listen(8080);
27+
```
28+
29+
This will start a web server. The server is available to test by opening a browser and using the URL http://localhost:8080/index.html.
30+
31+
8. Inside the `createServer` function add code to fetch the current URL:
32+
33+
```
34+
var q = url.parse(req.url, true);
35+
var filename = "." + q.pathname;
36+
```
37+
38+
9. Inside the `createServer` function, after the the previous lines of code, add code to load the appropriate HTML file based on the URL. For example the URL `http://localhost:8080/index.html` will should the index.html file.
39+
40+
```
41+
fs.readFile(filename, function(err, data) {
42+
res.writeHead(200, {'Content-Type': 'text/html'});
43+
res.write(data);
44+
return res.end();
45+
});
46+
```
47+
48+
10. Inside the `readFile` function, add code that will display an error message in case the requested URL does not match an exsting file:
49+
50+
```
51+
if (err) {
52+
res.writeHead(404, {'Content-Type': 'text/html'});
53+
return res.end("404 Not Found");
54+
}
55+
```
56+
57+
11. To test your Node.js website, open up a terminal, use `cd` to navigate to your project folder, and use `node app.js` to start your file. Then open a browser and visit the URL http://localhost:8080/index.html..
58+
59+
## Tutorial Requirements:
60+
61+
* [Visual Studio Code](https://code.visualstudio.com/) or [Brackets](http://brackets.io/) (or any code editor)
62+
* [Filezilla](https://filezilla-project.org/) (or any FTP program)
63+
* [Node.js](https://nodejs.org/en/)
64+
65+
Full tutorial URL: https://codeadam.ca/learning/php/php-introduction.html
66+
67+
<a href="https://codeadam.ca">
68+
<img src="https://codeadam.ca/images/code-block.png" width="100">
69+
</a>
70+

0 commit comments

Comments
 (0)