Skip to content

Instantly share code, notes, and snippets.

@SafraPC
Created February 26, 2021 21:39
Show Gist options
  • Save SafraPC/d69a2e56b7e30ec95850ecb28237d51f to your computer and use it in GitHub Desktop.
Save SafraPC/d69a2e56b7e30ec95850ecb28237d51f to your computer and use it in GitHub Desktop.
Create Node app with Express Framework and Typescript
for create package.json
yarn init -y
convert typescript to js more fast than babel and add ts
yarn add -D typescript sucrase
create folder src and add server.ts there
create into package.json
"scripts": {
"dev":"nodemon src/server.ts",
"build":"sucrase ./src -d ./dist --transforms typescript,imports"
},
for what att in the archive, the sucrase will att the app
yarn add -D nodemon
create a file nodemon.json and add
{
"watch":["src"],
"ext":"ts",
"execMap":{
"ts":"sucrase-node src/index.ts"
}
}
for add eslint
yarn add -D eslint @typescript-eslint/eslint-plugin
for configure eslint
yarn eslint --init
Select last opc "To check syntax, find problems, and enforce code style
Select JavaScript
None of These
Yes
Node
Standard
JavaScript
Yes
delete package.lock.json
for get all alterations
yarn
inside .eslintrc in extends add
['plugin:@typescript-eslint/recommended','prettier/@typescript-eslint', 'standard']
for add Prettier
yarn add prettier eslint-config-prettier eslint-plugin-prettier -D
for add cors, mongoose and express in application
yarn add express cors mongoose
for add types
yarn add @types/express @types/cors @types/mongoose -D
create server.ts inside src and put
import app from "./app";
app.listen(3333);
create app.ts in src folder and add
import express from "express";
import cors from "cors";
import mongoose from "mongoose";
import routes from "./routes";
class App {
public express: express.Application;
constructor() {
this.express = express();
this.middlewares();
this.routes();
// this.database();
}
private middlewares(): void {
this.express.use(express.json());
this.express.use(cors());
}
// private database(): void {
// mongoose.connect("mongodb://localhost:27017/tsnode", {
// useNewUrlParser: true,
// });
// }
private routes(): void {
this.express.use(routes);
}
}
export default new App().express;
create folder schemas inside src and a file called User.ts
import { Schema, model, Document } from "mongoose";
interface UserInterface extends Document {
email?: string;
firstName?: string;
lastName?: string;
}
const UserSchema = new Schema(
{
email: String,
firstName: String,
lastName: String,
},
{ timestamps: true }
);
export default model<UserInterface>("User", UserSchema);
create a folder called controllers and a file called UserController.ts and put
import { Request, Response } from "express";
import User from "../schemas/User";
class UserController {
public async index(request: Request, response: Response): Promise<Response> {
const users = await User.find();
return response.json(users);
}
public async create(request: Request, response: Response): Promise<Response> {
const user = await User.create(request.body);
return response.json(user);
}
}
export default new UserController();
create routes.tsx inside src and put
import { Router } from "express";
import UserController from "./controllers/UserController";
const routes = Router();
routes.get("/users", UserController.index);
routes.post("/users", UserController.create);
export default routes;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment