Git Product home page Git Product logo

crud_using_typescript_and_mongooes's Introduction

CRUD_using_Typescript_and_Mongooes

Full process

  1. Step 1: Create a new folder for the project and open it in VS Code. run on terminal npm init -y create package.json file.

  2. Step 2: install this package.

install express ⇒ npm install express

install mongoose ⇒ npm install mongoose --save

install typescript ⇒ npm install typescript --save-dev

install cors ⇒ npm install cors

install dotenv ⇒ npm install dotenv

  1. Step 3: create a tsconfig file ⇒ tsc --init

set rootDir as src and outDir as dist

  1. Step 4: Connect Mongoose to the server
import mongoose from 'mongoose';

async function main() {
 try {
   await mongoose.connect(config.database_url as string);

   app.listen(config.port, () => {
     console.log(`Example app listening on port ${config.port}`);
   });
 } catch (error) {
   console.log(error);
 }
}

main();

SetUP Eslint and Prettier

  1. Step 5: add this in tsconfing.json file
"include": ["src"], // which files to compile
 "exclude": ["node_modules"], // which files to skip 
  1. Step 6: install eslint ⇒ npm install eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin --save-dev

  2. Step 7: create a configuration file ⇒ npx eslint --init and add this in .eslintrc
    "rules": {
    "no-unused-vars": "error",
    "no-unused-expressions": "error",
    "prefer-const": "error",
    "no-console": "warn",
    "no-undef": "error"
    }

  3. Step 8: create a .eslintignore file and add the folders or files you want to ignore
    node_modules
    dist

  4. Step 9: add this in package.json

/ package.json
{
 // ...
 "scripts": {
   "start:prod": "node ./dist/server.js",
   "start:dev": "ts-node-dev --respawn --transpile-only src/server.ts",
   "build": "tsc",
  },
 // ...
}
  1. Step 10: fix errors in eslint, add this in package.json
/ package.json
{
  // ...
  "scripts": {
    "lint": "eslint src --ignore-path .eslintignore --ext .ts",
    "lint:fix": "npx eslint src --fix",
   },
  // ...
} 

setup prettier

  1. Step 11: install prettier ⇒ npm install --save-dev prettier
  2. Step 12: create a file called .prettierrc.json
{
    "semi": true,
    "singleQuote": true
}
  1. Step 13: add this in package.json
/ package.json
{
 // ...
 "scripts": {
   "prettier": "prettier --ignore-path .gitignore --write \"**./src/*.+(js|ts|json)\"",
   "prettier:fix": "npx prettier --write src",
  },
 // ...
}
  1. Step 14: you can open your command palette (Command + Shift + P) and look for Preferences: Open User Settings (JSON).
// settings.json
{
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.formatOnSave": true,
  ...
}
  1. Step 15: Avoiding conflicts when working with ESLint and Prettier ⇒ npm install --save-dev eslint-config-prettier

  2. Step 16: install ts node dev ⇒ npm i ts-node-dev --save-dev

  3. Step 17: add this code in package.json =>

 / package.json
{
 // ...
 "scripts": {
   "start:prod": "node ./dist/server.js",
   "start:dev": "ts-node-dev --respawn --transpile-only src/server.ts",
  },
 // ...
}
  1. Step 18: run server ⇒ npm run start:dev

crud_using_typescript_and_mongooes's People

Watchers

Rimon Uddin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.