Skip to content

Instantly share code, notes, and snippets.

@gcoonrod
Created November 7, 2019 23:26
Show Gist options
  • Save gcoonrod/d16e312a1f79c12e04a69a28bb3221c2 to your computer and use it in GitHub Desktop.
Save gcoonrod/d16e312a1f79c12e04a69a28bb3221c2 to your computer and use it in GitHub Desktop.
Actionhero and Expess comparision
// 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