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);```