Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

priantiko #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
6 changes: 5 additions & 1 deletion middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const logger = (req, res, next) => {

const notFoundHandler = (req, res, next) => {
res.status(404).send("URL not found");
next()
}

module.exports = {logger, notFoundHandler};
const notFoundHandler505 = (err, req, res, next) => {
res.status(505).send("Server Down Error 505");
}
module.exports = {logger, notFoundHandler,notFoundHandler505};
26 changes: 22 additions & 4 deletions routes.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const express = require('express');
const newsApi = require('newsapi');

const news = new newsApi('YOUR_API_HERE');
const check = require('./middlewares.js')
const app = express()
const news = new newsApi('04d34934928e42f8beff8e186d048899');
const router = express.Router();
app.use(check.logger)

// basic routing
router.get('/news', (req, res) => {
router.get('/news',check.logger, (req, res) => {
news.v2.topHeadlines({
q: req.query.q,
country: 'id'
Expand All @@ -15,9 +18,24 @@ router.get('/news', (req, res) => {
articles
})
} else {
res.send('Cannot fetch news.')
res.send("error")
}
});
})
router.get('/covid',check.logger, (req, res) => {
news.v2.everything({
q: 'covid',
language: 'id',
}).then(response => {
let articles = response.articles;
res.render('news', {
articles
})
});
})

router.get('/error',(req,res)=>{
iniError
})

module.exports = router;
7 changes: 6 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
const express = require('express');
const router = require('./routes');

const check = require('./middlewares')

// inisialisasi app
const app = express();
const port = process.env.PORT || 5000;
app.use(express.static(__dirname + '/public'));
app.set('view engine', 'ejs');
app.use(router);

app.use(router)
app.use(check.notFoundHandler)
app.use(check.notFoundHandler505)
// Body parser, reading data from body into req.body
app.use(express.json());

Expand Down
2 changes: 1 addition & 1 deletion views/news.ejs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<html>
<head>
<title>News</title>
<link rel="stylesheet" href="public/css/style.css">
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<div class="row">
Expand Down