Node.js te passwordu eşleştirdikten sonra istediğim html sayfasına nasıl yönlendirebilirim?

Node.js de user password ün eşleşmesine göre sayfa yönlendirmesi yapmak ve jsondaki veriyi bir html sayfasında listelemek istiyorum ama bir türlü başaramadım.Bu konuda bilgisi olan biri yardımcı olabilirse çok sevinirim.

var http = require('http'),
    url = require('url');
const express = require("express");
const app = express();
const port = 3000;

var users = [
               { "username":"admn", "password": "admn", "role": "admin" },
               { "username":"teachr", "password": "teachr", "role": "teacher" },
               { "username":"studnt", "password": "studnt", "role": "student" }
            ]

var data = [
              { "Projectname":"sdfsf", "projectteacher": "sdfsd", "thestudentthatgottheproject": "tsdf" },
              { "Projectname":"sdfsf", "projectteacher": "sdfsd", "thestudentthatgottheproject": "tsdf" },
              { "Projectname":"sdfsf", "projectteacher": "sdfsd", "thestudentthatgottheproject": "tsdf" }
           ]
function handle_incoming_request(req, res) {
    console.log("INCOMING REQUEST: " + req.method + " " + req.url);
    req.parsed_url = url.parse(req.url, true);
    var core_url = req.parsed_url.pathname;
    if (core_url == '/anasayfa.html') {
       res.writeHead(200, { "Content-Type" : "text/html" });
       res.end('<!DOCTYPE html><html><head><title>mainpage</title></head><body><form action="http://localhost:8080/ucheck.html" method="get"><input type="text" name="username"><input type="password" name="password"><input type="submit" value="sdfs"></form></body></html>');
    }
    else if (core_url == '/ur_check.html') {
      var getp = req.parsed_url.query;
      console.log(getp.username + " " + getp.password);
      for(i=0; i<users.length; i++) {
        if( (users[i].username == getp.username) && (users[i].password == getp.password)){
           switch ( users[i].role ){
              case "admin"        :
  const express = require("express");
const app = express();

app.get("/AnaSayfa.html",(req,res) => {
    res.send("Yönlendirildiniz !");
});

app.get("/",(req,res) => {
    console.log("Yönlendirme yapıldı");
    res.redirect(301,"http://localhost:5000/AnaSayfa.html");
});

app.listen(5000,() => {
    console.log("Server başlatıldı.");
});
             break;
            
           }
            res.writeHead(200, { "Content-Type" : "application/json" });
            res.end(JSON.stringify( { result: "FOUND", username: users[i].username, password: users[i].password, role: users[i].role }) + "\n");

            return;
          }
        }
      res.writeHead(404, { "Content-Type" : "application/json" });
      res.end(JSON.stringify({ result: "NOT FOUND", message: "USERNAME OR PASSWORD INVALID" }) + "\n");
   }
   else {
     res.writeHead(404, { "Content-Type" : "application/json" });
     res.end(JSON.stringify({ error: "NOT SUPPORTED YET", message: "REQUESTED PAGE DOES NOT EXIST" }) + "\n");
  }
var s = http.createServer(handle_incoming_request);
s.listen(8080);```

Yönlendirme kullanabilirsiniz:

Teşekkürler cevabınız için yönlendirdiğiniz siteye göre düzenlemeye çalıştım ama başarılı olamadım.Hatamı söyleyebilir misiniz acaba.Kodu editleyeceğim tekrar.@ Wormer_King

Burda olup sizin kodunuzda olmayan nedir ?
Bu kod sorunsuz çalışıyor:

const express = require("express");
const app = express();

app.get("/deneme",(req,res) => {
    res.send("Yönlendirildiniz !");
});

app.get("/",(req,res) => {
    console.log("Yönlendirme yapıldı");
    res.redirect(301,"http://localhost:5000/deneme");
});

app.listen(5000,() => {
    console.log("Server başlatıldı.");
});

Ve sizin kodunuzdaki hata mesajını paylaşabilirmisiniz.

Tekrar son halini paylaşacağım kodların.Hata mesajı:SyntaxError: Unexpected end of input
ama şöyle bir şey var kodun sonunda zaten 8080 portunu dinliyor.O bölümü mü uyarlamam gerekiyor acaba?@ Wormer_King

O kısmı app.listen(8000); olarak değiştirip deneyin bi.

O şekilde de denedim olmadı.Kodumda başka hatalar da varmış.Başka bir yöntemle kodlayacağım.Tekrar çok teşekkürler cevabınız için.@ Wormer_King

1 Beğeni