Created
September 28, 2017 19:10
-
-
Save yhaskell/d4574345f4ee4989dfdef8c2e5e52dac to your computer and use it in GitHub Desktop.
Minimal express api
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
import { Model, model, connect, primary } from 'mysql-records' | |
import { createHash } from 'crypto' | |
const dbc = connect('localhost', 'mysql-user', 'mysql-password', 'mysql-db') | |
@model(dbc, 'users') | |
export class User extends Model { | |
@primary id: number | |
username: string | |
password: string | |
static setPassword(user: User, newPassword: string) { | |
user.password = createHash('sha256').update(newPassword).digest('hex') | |
} | |
} |
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
import * as express from 'express' | |
import * as logger from 'morgan' | |
import * as compression from 'compression' | |
import * as bodyparser from 'body-parser' | |
import { User } from './models' | |
const eapp = express() | |
eapp.use(logger("dev")) | |
.use(compression()) | |
.use(bodyparser.urlencoded()) | |
.use(bodyparser.json()) | |
eapp.get('/api/v1/users', async (req, rsp) => { | |
const users = await User.all() | |
rsp.status(200).json(users.map(user => ({ ...user, password: undefined }))) | |
}) | |
eapp.listen(8000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment