Created
November 7, 2019 23:26
-
-
Save gcoonrod/d16e312a1f79c12e04a69a28bb3221c2 to your computer and use it in GitHub Desktop.
Actionhero and Expess comparision
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
// Actionhero Action | |
const {Action, api} = require('actionhero') | |
module.exports = class GetUser extends Action { | |
constructor () { | |
super() | |
this.name = 'getUser' | |
this.description = 'Get a user by their ID.' | |
this.inputs = { | |
id: { | |
required: true | |
} | |
} | |
this.outputExample = { | |
name: 'Bilbo', | |
age: 111, | |
ringBearer: true | |
} | |
} | |
async run (data) { | |
let user = await api.db.Users.findById(data.params.id) | |
if (!user) { | |
throw NotFoundError(id) \\ Just an example of a custom error. | |
} | |
return user | |
} | |
} | |
// Express Route | |
const Users = require('../models/user') | |
app.get('/users/:userId', function (req, res) { | |
Users.findById(req.params.userId) | |
.then(user => { | |
if (!user) { | |
res.status(404).send(`User ${req.params.userId} not found`) | |
} | |
res.send(user) | |
}) | |
.catch(error => { | |
console.error(error) | |
res.status(500).send('Internal Server Error') | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment