From c7ddb6ee2dd04f306017290e922d2840f13f4ab0 Mon Sep 17 00:00:00 2001 From: Vikita Windarwati Date: Fri, 28 Aug 2020 12:19:21 +0700 Subject: [PATCH] [QUIZ] push --- middlewares.js | 12 +++++++++++- package.json | 3 ++- routes.js | 20 +++++++++++++++++++- server.js | 24 ++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/middlewares.js b/middlewares.js index 0a225b7..b67bb00 100644 --- a/middlewares.js +++ b/middlewares.js @@ -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}; \ No newline at end of file +module.exports = {logger, internalServerError, notFoundHandler}; \ No newline at end of file diff --git a/package.json b/package.json index 08174cb..dc67d34 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/routes.js b/routes.js index cf2762e..e80faea 100644 --- a/routes.js +++ b/routes.js @@ -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, @@ -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; \ No newline at end of file diff --git a/server.js b/server.js index ff4a8cf..891b72e 100644 --- a/server.js +++ b/server.js @@ -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}`);