Skip to content

Commit

Permalink
Added Basic server setup in Backend Development
Browse files Browse the repository at this point in the history
  • Loading branch information
azizkhoso committed Oct 21, 2021
1 parent d024e65 commit 63f1d5f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Backend Development/Server Setup/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const http = require('http');

const server = http.createServer(/* callback function */ (req, res) => {
res.writeHead(200, {'Content-Type': 'text/html'}); // For every request the response's content will be in html or plain text
res.write(`
<head>
<title>Basic server setup in NodeJS</title>
</head>
`); // What ever is given to write method will be the content of web page

const { url } = req;
if(url === '/'){
res.write(`
<body>
<h1>Home page</h1>
<p>Welcome to my nodejs application.</p>
</body>
`); // You can use write method as many times as you want, but you can't after end method.
}
else if(url === '/about'){
res.write(`
<body>
<h1>About page</h1>
<p>Want to know about my web page! It's built in NodeJS.</p>
</body>
`);
}
else if(url === '/contact'){
res.write(`
<body>
<h1>Contact page</h1>
<p>You can reach me at [email protected].</p>
</body>
`);
}
else {
res.write(`
<body>
<h1>404, page not found</h1>
</body>
`);
}

res.end(); // end function is important for every request and response cycle to complete, else browser will be loading the response infinitely until the timeout.
});

server.listen(/* port */ 3000, /* callback function */ function() { // The server listens on port 3000, when ready the callback function will be executed
console.log('Server listening on port 3000.... navigate to http://localhost:3000/');
});
12 changes: 12 additions & 0 deletions Backend Development/Server Setup/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "server-setup",
"version": "1.0.0",
"description": "A basic server setup in NodeJS",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"author": "Abdul Aziz Khoso",
"license": "ISC"
}

0 comments on commit 63f1d5f

Please sign in to comment.