26
loading...
This website collects cookies to deliver better user experience
app.js
and server.js
, this became really helpful because you can have all your app routes and plugins initialized in a common build function in the app.js, and the app listening in the server.js.require("dotenv").config();
const fastify = require("fastify");
const cookie = require("fastify-cookie");
const { debug } = require("./routes/debug");
const { auth } = require("./routes/auth");
const { me } = require("./routes/me");
const build = (opts = {}) => {
const app = fastify(opts);
app.register(cookie);
app.register(debug);
app.register(me, { prefix: "/v2/me" });
app.register(auth, { prefix: "/v2/auth" });
return app;
};
module.exports = { build };
const { build } = require("./app.js");
const app = build({ logger: true });
app.listen(process.env.PORT || 5000, "0.0.0.0", (err, address) => {
if (err) {
console.log(err);
process.exit(1);
}
});
app.register(debug);
app.register(me, { prefix: "/v2/me" });
app.register(auth, { prefix: "/v2/auth" });
const me = (fastify, _, done) => {
fastify.addHook("onRequest", (request) => request.jwtVerify());
fastify.get("/", getMe);
fastify.put("/", putMeOpts, putMe);
done();
};
// ./utils/hash.js
const bcrypt = require("bcryptjs");
const hash = (plainText) => bcrypt.hashSync(plainText, 10);
const verify = (plainText, hashText) => bcrypt.compareSync(plainText, hashText);
module.exports = { hash, verify };
// ./lib/db.js
export async function deleteWebsite(seed) {
return new Website()
.where("seed", seed)
.destroy();
}
me.js => me.test.js
, and i recall at the build function on top of this article. Something like this:it("does login", async () => {
const app = await build();
const response = await app.inject({
method: "POST",
url: "/v2/auth/login",
payload: {
email: "[email protected]",
password: "password",
},
});
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.body)).toHaveProperty("access_token");
});