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

[QUIZ] push #3

Open
wants to merge 1 commit 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
12 changes: 11 additions & 1 deletion middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@ const logger = (req, res, next) => {
next();
}

const internalServerError = (err, req, res, next) => {
console.error(err)
res.status(500).json({
status: 'fail',
errors: err.message
})
next();
}

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

module.exports = {logger, notFoundHandler};
module.exports = {logger, internalServerError, notFoundHandler};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"cors": "^2.8.5",
"ejs": "^3.1.5",
"express": "^4.17.1",
"newsapi": "^2.4.1"
"newsapi": "^2.4.1",
"nodemon": "^2.0.4"
}
}
20 changes: 19 additions & 1 deletion routes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const express = require('express');
const newsApi = require('newsapi');

const news = new newsApi('YOUR_API_HERE');
// const app=express()
const news = new newsApi(`f32e8f88c36e4cabb88295d27dbe2416`);
const router = express.Router();
// basic routing
// app.use(express.static('public'))

router.get('/news', (req, res) => {
news.v2.topHeadlines({
q: req.query.q,
Expand All @@ -20,4 +23,19 @@ router.get('/news', (req, res) => {
});
})

router.get('/apple', (req, res) => {
news.v2.everything({
q: 'apple',
}).then(result => {
if(result.status === "ok") {
let articles = result.articles;
res.render('news', {
articles
})
} else {
res.send('Cannot fetch news.')
}
});
})

module.exports = router;
24 changes: 24 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
const express = require('express');
const router = require('./routes');
const path = require('path');
const {logger, internalServerError, notFoundHandler} = require('./middlewares');

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

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

//handle error 500 -coba
app.use(internalServerError);


// handle 404 dari notes kelas 5 kemarin
app.use(notFoundHandler);
// app.use(function(err, req, res, next) {
// console.error(err)
// res.status(500).json({
// status: 'fail',
// errors: err.message
// })
// })

// Body parser, reading data from body into req.body
app.use(express.json());
app.use(express.urlencoded({ extended: false }))

app.listen(port, () => {
console.log(`Application is running on port ${port}`);
Expand Down