-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Live service check on forum-proxy
- Loading branch information
Showing
1 changed file
with
41 additions
and
37 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,41 @@ | ||
import { fileURLToPath } from 'url'; | ||
import { dirname, join } from 'path'; | ||
|
||
import 'dotenv/config'; | ||
import cors from 'cors'; | ||
import fetch from 'node-fetch'; | ||
import express from 'express'; | ||
|
||
const app = express(); | ||
|
||
const __dirname = dirname(fileURLToPath(new URL(import.meta.url))); | ||
|
||
app.get('/', (req, res) => { | ||
res.sendFile(join(__dirname, './views/index.html')); | ||
}) | ||
|
||
app.use(cors({optionsSuccessStatus: 200})); | ||
app.get('/latest', (req, res, next) => { | ||
fetch('https://forum.freecodecamp.org/latest.json') | ||
.then(response => response.json()) | ||
.then(data => { | ||
res.json(data); | ||
}) | ||
.catch(err => next(err)); | ||
}); | ||
|
||
app.use((req, res) => res.status(404).send('not found')); | ||
|
||
app.use((err, req, res) => res.status(500).json(err)); | ||
|
||
const portNum = process.env.PORT || 3000; | ||
|
||
app.listen(portNum, function() { | ||
console.log(`Your app is listening on port ${portNum}`); | ||
}); | ||
|
||
export default app; | ||
import { fileURLToPath } from 'url'; | ||
import { dirname, join } from 'path'; | ||
|
||
import 'dotenv/config'; | ||
import cors from 'cors'; | ||
import fetch from 'node-fetch'; | ||
import express from 'express'; | ||
|
||
const app = express(); | ||
|
||
const __dirname = dirname(fileURLToPath(new URL(import.meta.url))); | ||
|
||
app.get('/', (req, res) => { | ||
res.sendFile(join(__dirname, './views/index.html')); | ||
}); | ||
|
||
app.get('/status/ping', (req, res) => { | ||
res.send({ msg: 'pong' }).status(200); | ||
}); | ||
|
||
app.use(cors({ optionsSuccessStatus: 200 })); | ||
app.get('/latest', (req, res, next) => { | ||
fetch('https://forum.freecodecamp.org/latest.json') | ||
.then(response => response.json()) | ||
.then(data => { | ||
res.json(data); | ||
}) | ||
.catch(err => next(err)); | ||
}); | ||
|
||
app.use((req, res) => res.status(404).send('not found')); | ||
|
||
app.use((err, req, res) => res.status(500).json(err)); | ||
|
||
const portNum = process.env.PORT || 3000; | ||
|
||
app.listen(portNum, function () { | ||
console.log(`Your app is listening on port ${portNum}`); | ||
}); | ||
|
||
export default app; |