Skip to content

Instantly share code, notes, and snippets.

View dimlucas's full-sized avatar

Dimitris Loukas dimlucas

View GitHub Profile
router.post("/", (req, res) => {
User.findOne({email: req.body.email}).then(user => {
//do something
}).catch(err => {
require("../../logger")(req).report(err);
});;
});
// File: A.js
console.log("In A.js");
class A {
construtor() {
//some logic
}
}
module.exports = A;
//Placing this before the RegisterServiceAsync is enough for the migrations to work
//No need to pass any arguments
BuildWebHost(new string[] { }).Run();
private static IWebHost BuildWebHost(string[] args)
{
return WebHost.CreateDefaultBuilder(args).UseStartup<Startup>().Build();
}
services.AddDbContext<AppDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("ToDos"));
});
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
public DbSet<ToDo> ToDos { get; set; }
}
module.exports.AsyncWrapper = function AsyncWrapper(fn) {
return (req, res, next) => {
return fn(req, res).catch(next);
}
}
import {RequestHandler} from 'express';
export function AsyncWrapper(fn: RequestHandler);
@dimlucas
dimlucas / request-handler.js
Created September 1, 2018 14:48
ExpressJS Request Handler type definition
export interface RequestHandler {
// tslint:disable-next-line callable-types (This is extended from and can't extend from a type alias in ts<2.2
(req: Request, res: Response, next: NextFunction): any;
}
//Assume it's in the same folder const asyncWrapper = require("./async-wrapper");
app.post("/path/to/endpoint", asyncWrapper(async (req, res) => {
//Do something like setting a header
res.setHeader("Content-type", "application/json");
}));