tada: Initial commit
This commit is contained in:
parent
19300b5352
commit
3529e9e4ee
32 changed files with 6714 additions and 1 deletions
57
packages/backend/src/index.js
Normal file
57
packages/backend/src/index.js
Normal file
|
@ -0,0 +1,57 @@
|
|||
import express from "express";
|
||||
import mongoose from "mongoose";
|
||||
import chalk from "chalk";
|
||||
import config from "./config";
|
||||
import middleware from "./middleware";
|
||||
import routes from "./routes";
|
||||
|
||||
const app = express();
|
||||
|
||||
// Load middleware
|
||||
for (let type of Object.keys(middleware)) middleware[type](app);
|
||||
|
||||
// Load routes
|
||||
for (let type of Object.keys(routes)) routes[type](app);
|
||||
|
||||
// Connecting to the database
|
||||
mongoose.Promise = global.Promise;
|
||||
mongoose
|
||||
.connect(
|
||||
config.db.uri,
|
||||
{
|
||||
useNewUrlParser: true
|
||||
}
|
||||
)
|
||||
.then(() => {
|
||||
console.log(chalk.green("Successfully connected to the database"));
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(
|
||||
chalk.red("Could not connect to the database. Exiting now..."),
|
||||
err
|
||||
);
|
||||
process.exit();
|
||||
});
|
||||
|
||||
app.get("/", async (req, res) => {
|
||||
try {
|
||||
const thing = await Promise.resolve({ one: "two" }); // async/await!
|
||||
return res.json({ ...thing, hello: "world" }); // object-rest-spread!
|
||||
} catch (e) {
|
||||
return res.json({ error: e.message });
|
||||
}
|
||||
});
|
||||
const port = process.env.PORT || 3000;
|
||||
|
||||
app.listen(port, err => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
}
|
||||
|
||||
if (__DEV__) {
|
||||
// webpack flags!
|
||||
console.log("> in development");
|
||||
}
|
||||
|
||||
console.log(`> listening on port ${port}`);
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue