Trying to build a web server using nodejs

Hello guys, sorry to disturb you all; I'm currently learning how to build a web server locally using nodejs. However, I have some doubts; I don't understand that part on the filePath, what are we trying to do here? Why are we both about only html files? What's happening here please
const extension = path.extname(req.url);
let contentType;

switch(extension){
case '.css':
contentType = 'text/css';
break;
case '.js':
contentType = 'text/javascript';
break;
case '.json':
contentType = 'application/json';
break
case '.jpeg':
contentType = 'image/jpeg';
break;
case '.png':
contentType = 'image/png';
break;
case '.txt':
contentType = 'text/plain'
break;
default:
contentType = 'text/html';
}

let filePath = contentType === 'text/html' && req.url === '/'
? path.join(__dirname, 'views.index.html')
: contentType === 'text/html' && req.url.slice(-1) === '/'
? path.join(__dirname,'views',req.url,'index.html')
: contentType === 'type/html'
? path.join(__dirname, 'views', req.url)
: path.join(__dirname, req.url);
const extension = path.extname(req.url);
let contentType;

switch(extension){
case '.css':
contentType = 'text/css';
break;
case '.js':
contentType = 'text/javascript';
break;
case '.json':
contentType = 'application/json';
break
case '.jpeg':
contentType = 'image/jpeg';
break;
case '.png':
contentType = 'image/png';
break;
case '.txt':
contentType = 'text/plain'
break;
default:
contentType = 'text/html';
}

let filePath = contentType === 'text/html' && req.url === '/'
? path.join(__dirname, 'views.index.html')
: contentType === 'text/html' && req.url.slice(-1) === '/'
? path.join(__dirname,'views',req.url,'index.html')
: contentType === 'type/html'
? path.join(__dirname, 'views', req.url)
: path.join(__dirname, req.url);
8 Replies
glutonium
glutonium•2mo ago
let filePath = contentType === 'text/html' && req.url === '/'
? path.join(__dirname, 'views.index.html')
: contentType === 'text/html' && req.url.slice(-1) === '/'
? path.join(__dirname,'views',req.url,'index.html')
: contentType === 'type/html'
? path.join(__dirname, 'views', req.url)
: path.join(__dirname, req.url);
let filePath = contentType === 'text/html' && req.url === '/'
? path.join(__dirname, 'views.index.html')
: contentType === 'text/html' && req.url.slice(-1) === '/'
? path.join(__dirname,'views',req.url,'index.html')
: contentType === 'type/html'
? path.join(__dirname, 'views', req.url)
: path.join(__dirname, req.url);
oh my GOD, just type if else statements 😂
let filePath = path.join(__dirname, req.url);

if (contentType === 'text/html' && req.url === '/')
filePath = path.join(__dirname, 'views.index.html');
else if (contentType === 'text/html' && req.url.slice(-1) === '/')
filePath = path.join(__dirname,'views',req.url,'index.html');
else if (contentType === 'type/html')
filePath = path.join(__dirname, 'views', req.url);
let filePath = path.join(__dirname, req.url);

if (contentType === 'text/html' && req.url === '/')
filePath = path.join(__dirname, 'views.index.html');
else if (contentType === 'text/html' && req.url.slice(-1) === '/')
filePath = path.join(__dirname,'views',req.url,'index.html');
else if (contentType === 'type/html')
filePath = path.join(__dirname, 'views', req.url);
i think u can write it as follows as well, i think it looks better this way
let filePath = path.join(__dirname, req.url);

if (contentType === 'text/html') {
filePath = filePath = path.join(__dirname, 'views', req.url);

if (req.url === '/')
filePath = path.join(__dirname, 'views.index.html');
else if (req.url.slice(-1) === '/')
filePath = path.join(__dirname,'views',req.url,'index.html');
}
let filePath = path.join(__dirname, req.url);

if (contentType === 'text/html') {
filePath = filePath = path.join(__dirname, 'views', req.url);

if (req.url === '/')
filePath = path.join(__dirname, 'views.index.html');
else if (req.url.slice(-1) === '/')
filePath = path.join(__dirname,'views',req.url,'index.html');
}
now i assume this is probably setting up the path to the file that the server is about to send. so , client side can ask for html contents to the server now these static html files are kept in the views folder and here it is just setting up the path to the html file that the client is asking for so the server can send to the client the html file is kept inside the views directory and this is all kept in the server and not in the client. but the server needs to know which html file needs to be sent and WHERE that html file is . the block of code simply sets up that path __dirname i believe is the global node variable that contains the path to the current directory
Faker
FakerOP•2mo ago
yep I see, why are we only interested in html file, like contentType is only for html
glutonium
glutonium•2mo ago
No description
glutonium
glutonium•2mo ago
the pwd command returns the path to the current directory it is a shell command below u can see i am running the js in node , and the js code is just logging out the __dirname which returns the same thing /home/glutonium/dirname i am not 100% sure. I don't have too much BE knowledge, but have you checked the full code? maybe they are checking for other content types somewhere else?
Faker
FakerOP•2mo ago
hmm the other contents like css/image/js file are handled in the last condition I understand what the guy try to do but I don't know, it's not that clear in my head 😭 I didn't get the "Aha" moment in my head 😅
glutonium
glutonium•2mo ago
lol. i cant say much without the whole code, assuming i'd understand the whole code at all lol
Faker
FakerOP•2mo ago
I can share the video if you want :c
glutonium
glutonium•2mo ago
ooh its a video well naah forget it lol would've been faster if it was github xD
Want results from more Discord servers?
Add your server