You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// index.tsimportexpressfrom'express';import{json}from'body-parser';import{currentUserRouter}from'./routes/current-user';constapp=express();app.use(json());app.use(currentUserRouter);app.listen(3000,()=>{console.log('Listening on port 3000!!!!!!!!');});
// index.tsimportexpressfrom'express';import{json}from'body-parser';import{currentUserRouter}from'./routes/current-user';import{signinRouter}from'./routes/signin';import{signoutRouter}from'./routes/signout';import{signupRouter}from'./routes/signup';constapp=express();app.use(json());app.use(currentUserRouter);app.use(signinRouter);app.use(signoutRouter);app.use(signupRouter);app.listen(3000,()=>{console.log('Listening on port 3000!');});
importexpress,{Request,Response}from'express';import{body}from'express-validator';constrouter=express.Router();router.post('/api/users/signup',[body('email').isEmail().withMessage('Email must be valid'),body('password').trim().isLength({min: 4,max: 20}).withMessage('Password must be between 4 and 20 characters')],(req: Request,res: Response)=>{const{ email, password }=req.body;if(!email||typeofemail!=='string'){res.status(400).send('Provide a valid email');}// new User({ email, password })});export{routerassignupRouter};
importexpress,{Request,Response}from'express';import{body,validationResult}from'express-validator';constrouter=express.Router();router.post('/api/users/signup',[body('email').isEmail().withMessage('Email must be valid'),body('password').trim().isLength({min: 4,max: 20}).withMessage('Password must be between 4 and 20 characters')],(req: Request,res: Response)=>{consterrors=validationResult(req);if(!errors.isEmpty()){returnres.status(400).send(errors.array());}const{ email, password }=req.body;console.log('Creating a user...')res.send({});});export{routerassignupRouter};
// request-validation-error.tsimport{ValidationError}from'express-validator';exportclassRequestValidationErrorextendsError{constructor(publicerrors: ValidationError[]){super();// Only because we are extending a built in classObject.setPrototypeOf(this,RequestValidationError.prototype)}}
// database-connection-error copy.tsexportclassDatabaseConnectionErrorextendsError{reason='Error connecting to database'constructor(){super();// Only because we are extending a built in classObject.setPrototypeOf(this,DatabaseConnectionError.prototype)}}
import{Request,Response,NextFunction}from'express';import{RequestValidationError}from'../errors/request-validation-error';import{DatabaseConnectionError}from'../errors/database-connection-error copy';exportconsterrorHandler=(err: Error,req: Request,res: Response,next: NextFunction)=>{if(errinstanceofRequestValidationError){console.log('handling this error as a request validation error')}if(errinstanceofDatabaseConnectionError){console.log('handling this error as a database connection error')}res.status(400).send({message: err.message});};
// database-connection-error.tsexportclassDatabaseConnectionErrorextendsError{statusCode=500;reason='Error connecting to database'constructor(){super();// Only because we are extending a built in classObject.setPrototypeOf(this,DatabaseConnectionError.prototype)}serializeErrors(){return[{message: this.reason}];}}
// request-validation-error.tsimport{ValidationError}from'express-validator';exportclassRequestValidationErrorextendsError{statusCode=400;constructor(publicerrors: ValidationError[]){super();// Only because we are extending a built in classObject.setPrototypeOf(this,RequestValidationError.prototype)}serializeErrors(){returnthis.errors.map(error=>{return{message: error.msg,field: error.param};});}}
import{ValidationError}from'express-validator';interfaceCustomError{statusCode: number;serializeErrors(): {message: string;field?: string;}[]}exportclassRequestValidationErrorextendsErrorimplementsCustomError{statusCode=400;constructor(publicerrors: ValidationError[]){super();// Only because we are extending a built in classObject.setPrototypeOf(this,RequestValidationError.prototype)}serializeErrors(){returnthis.errors.map(error=>{return{message: error.msg,field: error.param};});}}
// database-connection-error.tsimport{CustomError}from'./custom-error';exportclassDatabaseConnectionErrorextendsCustomError{statusCode=500;reason='Error connecting to database'constructor(){super('Error connecting to db');// Only because we are extending a built in classObject.setPrototypeOf(this,DatabaseConnectionError.prototype)}serializeErrors(){return[{message: this.reason}];}}
// request-validation-error.tsimport{ValidationError}from'express-validator';import{CustomError}from'./custom-error';exportclassRequestValidationErrorextendsCustomError{statusCode=400;constructor(publicerrors: ValidationError[]){super('Invalid request parameters');// Only because we are extending a built in classObject.setPrototypeOf(this,RequestValidationError.prototype)}serializeErrors(){returnthis.errors.map(error=>{return{message: error.msg,field: error.param};});}}
import{CustomError}from'./custom-error';exportclassNotFoundErrorextendsCustomError{statusCode=404;constructor(){super('Route not found');Object.setPrototypeOf(this,NotFoundError.prototype);}serializeErrors(){return[{message: 'Not Found'}];}}
importexpressfrom'express';import{json}from'body-parser';import{currentUserRouter}from'./routes/current-user';import{signinRouter}from'./routes/signin';import{signoutRouter}from'./routes/signout';import{signupRouter}from'./routes/signup';import{errorHandler}from'./middleware/error-handler';import{NotFoundError}from'./errors/not-found-error';constapp=express();app.use(json());app.use(currentUserRouter);app.use(signinRouter);app.use(signoutRouter);app.use(signupRouter);app.all('*',()=>{thrownewNotFoundError();});app.use(errorHandler);app.listen(3000,()=>{console.log('Listening on port 3000!');});