Kevin Powell - CommunityKP-C
Kevin Powell - Community15mo ago
24 replies
Faker

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);
Was this page helpful?