0% found this document useful (0 votes)
10 views2 pages

Node Js

The document outlines a simple Node.js application structure with an HTTP server that responds to different routes. It includes a utility function for getting the current time, serves a welcome message and an about page, and reads data from a JSON file. Error handling is implemented for file reading and for unknown routes, ensuring a basic user experience.

Uploaded by

noxadi3388
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Node Js

The document outlines a simple Node.js application structure with an HTTP server that responds to different routes. It includes a utility function for getting the current time, serves a welcome message and an about page, and reads data from a JSON file. Error handling is implemented for file reading and for unknown routes, ensuring a basic user experience.

Uploaded by

noxadi3388
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

my-node-app/

├── app.js
├── data.json
└── utils.js

{
"message": "Hello from JSON!",
"status": "success"
}

// utils.js
function getCurrentTime() {
return new Date().toLocaleString();
}

module.exports = {
getCurrentTime
};

// app.js
const http = require(’http’);
const fs = require(’fs’);
const path = require(’path’);
const { getCurrentTime } = require(’./utils’);

const PORT = 3000;

const server = http.createServer((req, res) => {


const url = req.url;

if (url === ’/’) {


res.writeHead(200, { ’Content-Type’: ’text/html’ });
res.end(‘
<h1>Welcome to My Node App</h1>
<p>Current server time: ${getCurrentTime()}</p>
‘);
} else if (url === ’/about’) {
res.writeHead(200, { ’Content-Type’: ’text/html’ });
res.end(’<h1>About Page</h1><p>This is a simple Node.js server.</p>’);
} else if (url === ’/data’) {
fs.readFile(path.join(__dirname, ’data.json’), ’utf8’, (err, data) => {
if (err) {
res.writeHead(500, { ’Content-Type’: ’application/json’ });
res.end(JSON.stringify({ error: ’Failed to load data.’ }));
return;
}
res.writeHead(200, { ’Content-Type’: ’application/json’ });
res.end(data);
});
} else {
res.writeHead(404, { ’Content-Type’: ’text/html’ });
res.end(’<h1>404 - Page Not Found</h1>’);
}
});

server.listen(PORT, () => {
console.log(‘Server running at http://localhost:${PORT}‘);
});

| Concept | Explanation |
| -------------- | ----------------------------------------------- |
| ‘http‘ module | Used to create a web server. |
| ‘fs‘ module | Reads a local file (‘data.json‘). |
| Routing | Handles different URLs: ‘/‘, ‘/about‘, ‘/data‘. |
| Modularization | Imports ‘getCurrentTime‘ from ‘utils.js‘. |
| JSON handling | Serves JSON data via ‘/data‘ route. |
| Error handling | Simple handling for missing file. |

You might also like