Solution 1 :

Since your route is only /#contact your url should be http://localhost:3000/#contact. I would advise not to use /#contact but, instead, /contact as # is used to handle id anchors in the frontend.

Problem :

I have an issue with switching to other pages in node js.Here is my code below

//here are my global variables
const express = require('express');
var app = require("express")(); 
var bodyParser = require("body-parser"); 
const port = 3000;
var http=require('http');
var url=require('url');

//code
var server=http.createServer(function(req,res){
    var pathname=url.parse(req.url).pathname;
    switch(pathname){
        case '/whatYouGot':
            res.sendFile('whatYouGot.html');
        break;
        case '/#contact':
            res.sendFile('index.html');
        break;
        case '/#about' :
            res.sendFile('index.html');
        break;
        default:
            res.end('default');
        break;
    }

})
app.listen(port, () => console.log("example app listening on port %s",port));

I am not able to switch pages as I am getting an error like
Cannot GET /index.html
here is my url:
http://localhost:3000/index.html#contact
How can I resolve this?

By

Leave a Reply

Your email address will not be published. Required fields are marked *