npm install
npm start
Instead of npm start
, use:
npm run dev
This will restart the server whenever you change a file. It also creates and uses a fake mongodb instance in memory with data in fake_db.json
. To use a real database, use npm run dev-real
instead.
If you want to specify specific ids to documents in fake_db.json
, you need to provide real ids that mongodb likes. You can use npm run randomid
to generate a random id compatible with mongodb.
Note: Please install the Prettier Plugin on your preferred text editor, and set it up to format on save. This will automatically format the code to be more readable.
npm test
Change these environment variables before running the server:
PORT=9000 # Port the server will run on.
MONGO=mongodb:https://localhost:27017/db # URL of the mongo instance
DB=memory # Use in-memory mongodb instance (Overrides MONGO=...)
DATA=fake # At startup and upon calls to GET /reset, loads the database with data from fake_db.json instead of empty data.
// Usage:
const auth = require('./auth')
// Without auth:
app.get((req, res) => {
res.send('Hello world!')
})
// With auth (for any logged in user):
app.get(auth.any((req, res) => {
const currentUser = req.user
res.send('Hello ' + currentUser.username)
}))
// With auth (merchants only):
app.get(auth.merchant((req, res) => {
const currentUser = req.user
res.send('Hello ' + currentUser.username)
}))
// Note: You can also use: auth.customer and auth.robot in this way
// Select your own user groups:
app.get(auth(['merchant', 'robot'], (req, res) => {
const currentUser = req.user
res.send('Hello ' + currentUser.username)
}))