Last active
February 10, 2021 12:03
-
-
Save shalaby/270856f969b54f70381117d5ab2f319a to your computer and use it in GitHub Desktop.
Simple news server.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const app = express() | |
const akhbar = [] | |
app.get('/', function(req, res) { | |
const ahkbarHtml = akhbar.map(function(khabar, index) { | |
return ` | |
<a href="/${index}"><h3>${khabar.title}</h3></a> | |
<p>${khabar.content}</p> | |
` | |
}) | |
res.send(` | |
<h2>Akhbar</h2> | |
<h3>Add Khabar</h3> | |
<form action="/save"> | |
<input name="formTitle"> | |
<br> | |
<textarea name="formContent"></textarea> | |
<br> | |
<button>Send</button> | |
</form> | |
<div> | |
${ahkbarHtml.join('')} | |
</div> | |
`) | |
}) | |
app.get('/save', function(req, res) { | |
const enwanAlkhabar = req.query.formTitle | |
const nasAlkhabar = req.query.formContent | |
const khabarGadid = { | |
title: enwanAlkhabar, | |
content: nasAlkhabar | |
} | |
akhbar.push(khabarGadid) | |
res.redirect('/') | |
}) | |
app.get('/:x', function(req, res) { | |
const khabarIndex = req.params.x | |
const khabar = akhbar[khabarIndex] | |
const khabarHtml = ` | |
<h3>${khabar.title}</h3> | |
<p>${khabar.content}</p> | |
` | |
res.send(khabarHtml) | |
}) | |
app.listen(3000, function() { | |
console.log('Server is running ...') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment