Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export class Client {
* @return {Promise<string | null>} username
* */
async getUser() {
throw new Error("Not implemented");
return fetch('api/user').then(async res => {
return (await res.json())['username'];
})
}

/**
Expand All @@ -17,7 +19,12 @@ export class Client {
* @return {Promise<string | null>} username
* */
async loginUser(username) {
throw new Error("Not implemented");
return fetch(`api/login?username=${username}`).then(async res => {
let json = await res.json();
let username = json['username'];
console.log("client/login: " + username)
return username;
})
}

/**
Expand All @@ -26,7 +33,7 @@ export class Client {
* @return {void}
* */
async logoutUser() {
throw new Error("Not implemented");
await fetch('api/logout');
}

/**
Expand All @@ -49,8 +56,8 @@ export class Client {
* @property {string} summary
* @return {Promise<About>}
* */
async getInfo() {
throw new Error("Not implemented");
async getInfo() { //let response =
return (await fetch('https://api.spacexdata.com/v3/info')).json();
}

/**
Expand All @@ -63,7 +70,7 @@ export class Client {
* @return {Promise<EventBrief[]>}
* */
async getHistory() {
throw new Error("Not implemented");
return (await fetch('https://api.spacexdata.com/v3/history')).json();
}

/**
Expand All @@ -80,7 +87,7 @@ export class Client {
* @return {Promise<EventFull>}
* */
async getHistoryEvent(id) {
throw new Error("Not implemented");
return (await fetch(`https://api.spacexdata.com/v3/history/${id}`)).json();
}

/**
Expand All @@ -93,7 +100,7 @@ export class Client {
* @return {Promise<RocketBrief[]>}
* */
async getRockets() {
throw new Error("Not implemented");
return (await fetch('https://api.spacexdata.com/v3/rockets')).json();
}

/**
Expand All @@ -118,7 +125,7 @@ export class Client {
* @return {Promise<RocketFull>}
* */
async getRocket(id) {
throw new Error("Not implemented");
return (await fetch(`https://api.spacexdata.com/v3/rocket/${id}`)).json();
}

/**
Expand All @@ -135,7 +142,7 @@ export class Client {
* @return {Promise<Roadster>}
* */
async getRoadster() {
throw new Error("Not implemented");
return (await fetch('https://api.spacexdata.com/v3/roadster')).json();
}

/**
Expand All @@ -152,7 +159,7 @@ export class Client {
* @return {Promise<Item[]>}
* */
async getSentToMars() {
throw new Error("Not implemented");
throw new Error("Not implemented");
}

/**
Expand All @@ -170,7 +177,7 @@ export class Client {
* @return {Promise<Item[]>}
* */
async sendToMars(item) {
throw new Error("Not implemented");
throw new Error("Not implemented");
}

/**
Expand All @@ -181,6 +188,6 @@ export class Client {
* @return {Promise<Item[]>}
* */
async cancelSendingToMars(item) {
throw new Error("Not implemented");
throw new Error("Not implemented");
}
}
}
111 changes: 56 additions & 55 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
},
"dependencies": {
"body-parser": "^1.19.0",
"cookie-parser": "^1.4.5",
"cookie-parser": "^1.4.6",
"cookies": "^0.8.0",
"express": "^4.17.1",
"node-fetch": "^2.6.1",
"nodemon": "^2.0.6"
Expand Down
54 changes: 52 additions & 2 deletions server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ const rootDir = process.cwd();
const port = 3000;
const app = express();

app.use(express.static('spa/build')); //1
app.use(cookieParser()); //5
app.use(bodyParser.json());

//7
app.use((req, res, next) => {
const cancelRedirect = ['api', 'static', 'login', 'client.mjs'];
const route = req.url.split("/")[1];

if (!req.cookies.username && !cancelRedirect.includes(route))
res.redirect("/login");
else next();
});

app.get("/client.mjs", (_, res) => {
res.header("Cache-Control", "private, no-cache, no-store, must-revalidate");
res.sendFile(path.join(rootDir, "client.mjs"), {
Expand All @@ -18,10 +32,46 @@ app.get("/client.mjs", (_, res) => {
});
});

app.get('', (req, res) => {
res.sendFile(path.join(rootDir, "spa/build/index.html"));
});

app.get("/", (_, res) => {
res.send(":)");
});

app.listen(port, () => {
console.log(`App listening on port ${port}`);
app.get("/api/login", (req,res)=>{
console.log("api/login: start" );
let username = req.query.username;
console.log("api/login: " + username);
res.cookie('username', username);
res.json({'username':username});
});

app.get("/api/user", (req, res) => {
let username = req.cookies.username;
console.log("api/user: " + username);
res.json({'username':username});
});

app.get("/api/logout", (req, res)=>{
console.log('api/logout: start');
res.clearCookie('username');
res.redirect('/');
})

app.get('/*', (req, res) => {
res.redirect('/');
});


//3 task
https.createServer(
{
key: fs.readFileSync("certs/server.key"),
cert: fs.readFileSync("certs/server.cert"),
},
app
).listen(port, function () {
console.log(`Я работаю на ${port}`);
});