Skip to content

Instantly share code, notes, and snippets.

View conor-deegan's full-sized avatar
✌️

Conor Deegan conor-deegan

✌️
View GitHub Profile

Keybase proof

I hereby claim:

  • I am conor-deegan on github.
  • I am conordeegan (https://keybase.io/conordeegan) on keybase.
  • I have a public key ASDf5oR4mrVYMrJ8aapULxkQnRaVVMs58JNI5sd8vYnrCgo

To claim this, I am signing this object:

app.get('/', (req, res, next) => {
fs.readFile('/file-does-not-exist', (err, data) => {
if (err) {
// This will pass this error directly to our error handler
next(err)
} else {
res.status(200).json(data);
}
})
})
@conor-deegan
conor-deegan / decentralised.js
Last active September 14, 2020 15:17
Decentralised Error Handling in Express
app.get('/', (req, res) => {
fs.readFile('/file-does-not-exist', (err, data) => {
if (err) {
// Return an error directly from the route
res.status(404).json(err);
} else {
res.status(200).json(data);
}
})
})
import React, { Component } from 'react';
import './App.css';
import axios from 'axios'
class App extends Component {
state = {
response: {}
};
componentDidMount() {
const express = require('express');
const router = express.Router();
const controllers = require('./../controllers/controllers');
router.get('/say-something', controllers.saySomething);
module.exports = router;
const saySomething = (req, res, next) => {
res.status(200).json({
body: 'Hello from the server!'
});
};
module.exports.saySomething = saySomething;
// Import dependencies
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require('path');
// Create a new express application named 'app'
const app = express();
// Set our backend port to be either an environment variable or port 5000