Created
March 5, 2022 13:42
-
-
Save full-stack-concepts/4718a8eac99fca98d946a1ce9a5361af to your computer and use it in GitHub Desktop.
This file contains hidden or 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 express from "express"; | |
import cors from 'cors'; | |
import morgan from "morgan"; | |
import { Low, JSONFile } from 'lowdb'; | |
import userRouter from './users.js'; | |
import { nanoid } from 'nanoid'; | |
const adapter = new JSONFile("db.json"); | |
const db = new Low(adapter); | |
db.data = ({ users: [ | |
{ | |
id: 1, | |
role: 'admin', | |
email: '[email protected]' , | |
password: '12345678', | |
firstName: 'Admin', | |
lastName: 'Adminstrator', | |
token: nanoid(30) | |
}, | |
{ | |
id: 2, | |
role: 'user', | |
email: '[email protected]', | |
password: '12345678', | |
firstName: 'John', | |
lastName: 'Doe', | |
token: nanoid(30) | |
} | |
]}); | |
await db.write(db.data); | |
const PORT = process.env.PORT || 4000 | |
const app = express(); | |
app.db = db; | |
app.use(cors({origin: '*'})); | |
app.use(express.json()); | |
app.use(morgan("dev")); | |
app.use("/users", userRouter); | |
const localRouter = express.Router(); | |
localRouter.get("/", (req, res) => { | |
res.send('Only /users/* routes are supported '); | |
}); | |
app.use(localRouter); | |
app.listen(PORT, () => console.log(`Listening on Port ${PORT}`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment