{"id":26871194,"url":"https://github.com/codeadamca/nodejs-website","last_synced_at":"2025-05-07T06:29:20.661Z","repository":{"id":115328367,"uuid":"291393048","full_name":"codeadamca/nodejs-website","owner":"codeadamca","description":"This is a sample basic Node.js website using plain HTML files and the built in http and fs modules.","archived":false,"fork":false,"pushed_at":"2025-01-26T22:16:10.000Z","size":24,"stargazers_count":2,"open_issues_count":0,"forks_count":5,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-31T07:18:41.020Z","etag":null,"topics":["filesystem","http","learning-code","nodejs","urls"],"latest_commit_sha":null,"homepage":"","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codeadamca.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-30T03:29:40.000Z","updated_at":"2025-01-26T22:16:13.000Z","dependencies_parsed_at":null,"dependency_job_id":"4e7829ea-826c-4b26-b8f1-ac6004cef37a","html_url":"https://github.com/codeadamca/nodejs-website","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fnodejs-website","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fnodejs-website/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fnodejs-website/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codeadamca%2Fnodejs-website/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codeadamca","download_url":"https://codeload.github.com/codeadamca/nodejs-website/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252826491,"owners_count":21810121,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["filesystem","http","learning-code","nodejs","urls"],"created_at":"2025-03-31T07:18:44.070Z","updated_at":"2025-05-07T06:29:20.636Z","avatar_url":"https://github.com/codeadamca.png","language":"HTML","readme":"# A Sample Node.js Website\n\nThis is a sample basic Node.js website using plain HTML files and the built in http and fs modules.\n\nThis 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:\n\n## Steps\n\n1. Create a folder for your Node.js project.\n2. Create a home page HTML and call it `index.html`. Add the required HTML tags and some basic content in the `index.html` file. \n3. 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.\n4. Add a link in the `index.html` page to `another-page.html`. And add a link in the `another-page.html` to `index.html`. This will allow for easy testing of pages.\n5. Create a new file called `app.js`. \n6. In `app.js` import the required modules:\n\n      ```js\n      var http = require('http');\n      var url = require('url');\n      var fs = require('fs');\n      ```\n\n7. Create an http server:\n\n      ```js\n      http.createServer(function (req, res) {\n      }).listen(8080);\n      ```\n\n      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`.\n\n8. Inside the `createServer` function add code to fetch the current URL:\n      \n      ```js\n      var q = url.parse(req.url, true);\n      var filename = \".\" + q.pathname;\n      ```\n   \n9. 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 load the `index.html` file.\n      \n      ```js\n      fs.readFile(filename, function(err, data) {\n        res.writeHead(200, {'Content-Type': 'text/html'});\n        res.write(data);\n        return res.end();\n      });\n      ```\n\n10. Inside the `readFile` function, add code that will display an error message in case the requested URL does not match an exsting file:\n      \n      ```js \n      if (err) {\n        res.writeHead(404, {'Content-Type': 'text/html'});\n        return res.end(\"404 Not Found\");\n      } \n      ```\n\n11. 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`.\n\n## Final Code\n\nYour final code in `app.js` should look like this:\n\n```js\nvar http = require('http');\nvar url = require('url');\nvar fs = require('fs');\n\nhttp.createServer(function (req, res) {\n\n  var q = url.parse(req.url, true);\n\n  var filename = \".\" + q.pathname;\n\n  fs.readFile(filename, function(err, data) {\n\n    if (err) {\n\n      res.writeHead(404, {'Content-Type': 'text/html'});\n      return res.end(\"404 Not Found\");\n\n    } \n\n    res.writeHead(200, {'Content-Type': 'text/html'});\n    res.write(data);\n    return res.end();\n\n  });\n\n}).listen(8080);\n```\n\n\u003e Full tutorial URL:  \n\u003e https://codeadam.ca/learning/nodejs-website.html\n\n***\n\n## Repo Resources\n\n* [Visual Studio Code](https://code.visualstudio.com/)\n* [Node.js](https://nodejs.org/en/)\n\n\u003cbr\u003e\n\u003ca href=\"https://codeadam.ca\"\u003e\n\u003cimg src=\"https://cdn.codeadam.ca/images@1.0.0/codeadam-logo-coloured-horizontal.png\" width=\"200\"\u003e\n\u003c/a\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeadamca%2Fnodejs-website","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodeadamca%2Fnodejs-website","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodeadamca%2Fnodejs-website/lists"}