Typescript With Mongoose and Node/Express

Typescript is a superset of javascript. when we are writing typescript we are still writing javascript, except in this case we are writing javascript with a type system. So, in Summary, Typescript is Javascript plus type checking. The final source code for this tutorial can be found HERE

Why Typescript?

Typescript introduced a type system to javascript and these are the advantages that bring to us during development.

  1. Typescript can help us catch errors during development
  2. Typescript makes our Javascript code easier to read and debug
  3. Typescript is open source.

There are many more reasons to use typescript, and you can find out more about that in this article: https://dzone.com/articles/what-is-typescript-and-why-use-it

This tutorial assumes you already have some knowledge of Node.js and you want to know how to use typescript with Node.js

Also, I Recommend VScode as a code editor because VSCode includes typescript language support.

Getting Started

Create a new folder for your app, you can call it anything, on your terminal navigate to the newly created folder and run this command

npm init -y

this will create a package.json file for us.

install the following dependencies

npm install typescript --save-dev

npm install express body-parser --save

The next thing is to create a tsconfig file for typescript,

run this command on the terminal

npx tsc --init

if you are having issues with this or this is throwing an error, install typescript globally like this:

npm install typescript -g

then run the command again,

This will create the** tsconfig** file for us, tsconfig specifies the root folder and the compiler option to compile our typescript code down to javascript. we can always edit this file to modify the default configuration, but weโ€™ll go with the default configuration in this tutorial.

Now that we have installed typescript and we have our configuration file, letโ€™s get started with creating our express server

Setup the server

create a new folder and call it **src, **in our **src **folder create a new file **index.ts, **open the file and add the following code:

Image for post

#mongodb #expressjs #typescript #javascript #nodejs

What is GEEK

Buddha Community

Typescript With Mongoose and Node/Express

Node.js Express EJS Layouts and Partials Tutorial

Today we are going to look at how we can use Express EJS Layouts to help us with website templating which ultimately help us to avoid writing duplicated code as well as making our website/application easily maintainable.

I had to cut off the intro as the music was too lound.

SOURCE FILES
https://raddy.co.uk/blog/nodejs-express-layouts-and-partials/

CONNECT with RaddyTheBrand
Website: https://www.raddy.co.uk
GitHub: https://www.github.com/RaddyTheBrand
Instagram: https://www.instagram.com/RaddyTheBrand
Twitter: https://www.twitter.com/RaddyTheBrand
Newsletter: https://www.raddy.co.uk/newsletter

DONATE to RaddyTheBrand
BuyMeACoffee: https://www.buymeacoffee.com/RaddyTheBrand
PayPal: https://bit.ly/3tAuElv

#partials #ejs #node.js #node #express #node.js express ejs layouts and partials tutorial

Alec  Nikolaus

Alec Nikolaus

1610609530

Setting up TypeScript and Node with Express

Hello, my friends and fellow developers, in this video, I have talked about setting up TypeScript and NodeJS with Express.

#typescript #node #express #nodejs #javascript

How to set up TypeScript with Node and Express

Learn how to set up TypeScript with Node and Express in this video.

๐Ÿ”ด Subscribe here: https://bit.ly/2K5Ydaf

Connect ๐Ÿ”— :
Twitter: https://twitter.com/MaxProgramming1
Facebook: https://www.facebook.com/Max-Programmโ€ฆ
Github: https://github.com/max-programming/

๐Ÿ”ฅ Timestamps Start ๐Ÿ”ฅ

0:00 - Introduction
1:45 - Setting up the node environment
7:39 - Creating NPM Scripts
9:12 - Configuring TypeScript with tsconfig.json

๐Ÿ”ฅ Timestamps End ๐Ÿ”ฅ

https://youtu.be/tHzmSuBRb0k

#node #typescript #javascript #express #backend

TypeScript Express Starter: Quick and Easy TypeScript Express Starter

TypeScript Express Starter 

๐Ÿš€ Express RESTful API Boilerplate Using TypeScript      

๐Ÿ˜Ž Introducing The Project

Express consists of JavaScript, which makes it vulnerable to type definitions.

That's why we avoid supersets with starter packages that introduce TypeScript.

The package is configured to use TypeScript instead of JavaScript.

The project referred to express-generator-typescript

๐Ÿค” What is Express ?

Express is a fast, open and concise web framework and is a Node.js based project.

๐Ÿš€ Quick Start

Install with the npm Global Package

$ npm install -g typescript-express-starter

Run npx to Install The Package

npx is a tool in the JavaScript package management module, npm.

This is a tool that allows you to run the npm package on a single run without installing the package.

If you do not enter a project name, it defaults to typescript-express-starter.

$ npx typescript-express-starter "project name"

Select a Templates

Example Cli

Start your typescript-express-starter app in development mode at http://localhost:3000/

Template Type

NameDescription
DefaultExpress Default
Routing ControllersCreate structured, declarative and beautifully organized class-based controllers with heavy decorators usage
SequelizeEasy to use multi SQL dialect ORM for Node.js
MongooseMongoDB Object Modeling(ODM) designed to work in an asynchronous environment
TypeORMAn ORM that can run in Node.js and Others
PrismaModern Database Access for TypeScript & Node.js
KnexSQL query builder for Postgres, MySQL, MariaDB, SQLite3 and Oracle
GraphQLquery language for APIs and a runtime for fulfilling those queries with your existing data
TypegooseDefine Mongoose models using TypeScript classes
Mikro ORM (Develop)TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, MariaDB, PostgreSQL and SQLite databases
Sequelize Typescript (Develop)Decorators and some other features for sequelize

๐Ÿ›Ž Available Commands for the Server

  • Run the Server in production mode : npm run start or Start typescript-express-starter in VS Code
  • Run the Server in development mode : npm run dev or Dev typescript-express-starter in VS Code
  • Run all unit-tests : npm test or Test typescript-express-starter in VS Code
  • Check for linting errors : npm run lint or Lint typescript-express-starter in VS Code
  • Fix for linting : npm run lint:fix or Lint:Fix typescript-express-starter in VS Code

๐Ÿ’Ž The Package Features

Application

DevOps

Utilities

Database

Simple Icons

๐Ÿณ Docker :: Container Platform

Docker is a platform for developers and sysadmins to build, run, and share applications with containers.

Docker Install.

  • starts the containers in the background and leaves them running : docker-compose up -d
  • Stops containers and removes containers, networks, volumes, and images : docker-compose down

Modify docker-compose.yml and Dockerfile file to your source code.

โ™ป๏ธ NGINX :: Web Server

NGINX is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache.

Proxying is typically used to distribute the load among several servers, seamlessly show content from different websites, or pass requests for processing to application servers over protocols other than HTTP.

When NGINX proxies a request, it sends the request to a specified proxied server, fetches the response, and sends it back to the client.

Modify nginx.conf file to your source code.

โœจ ESLint, Prettier :: Code Formatter

Prettier is an opinionated code formatter.

ESLint, Find and fix problems in your JavaScript code

It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary.

Install VSCode Extension Prettier, ESLint

CMD + Shift + P (Mac Os) or Ctrl + Shift + P (Windows)

Format Selection With

Configure Default Formatter...

Prettier - Code formatter

Formatter Setting

Palantir, the backers behind TSLint announced in 2019 that they would be deprecating TSLint in favor of supporting typescript-eslint in order to benefit the community. So, migration from TSLint to ESLint.

๐Ÿ“— Swagger :: API Document

Swagger is Simplify API development for users, teams, and enterprises with the Swagger open source and professional toolset.

Easily used by Swagger to design and document APIs at scale.

Start your app in development mode at http://localhost:3000/api-docs

Modify swagger.yaml file to your source code.

๐ŸŒ REST Client :: HTTP Client Tools

REST Client allows you to send HTTP request and view the response in Visual Studio Code directly.

VSCode Extension REST Client Install.

Modify *.http file in src/http folder to your source code.

๐Ÿ”ฎ PM2 :: Advanced, Production process manager for Node.js

PM2 is a daemon process manager that will help you manage and keep your application online 24/7.

  • production mode :: npm run deploy:prod or pm2 start ecosystem.config.js --only prod
  • development mode :: npm run deploy:dev or pm2 start ecosystem.config.js --only dev

Modify ecosystem.config.js file to your source code.

๐ŸŽ SWC :: a super-fast JavaScript / TypeScript compiler

SWC is an extensible Rust-based platform for the next generation of fast developer tools.

SWC is 20x faster than Babel on a single thread and 70x faster on four cores.

  • tsc build :: npm run build
  • swc build :: npm run build:swc

Modify .swcrc file to your source code.

๐Ÿ—‚ Code Structure (default)

โ”‚
โ”œโ”€โ”€ /.vscode
โ”‚   โ”œโ”€โ”€ launch.json
โ”‚   โ””โ”€โ”€ settings.json
โ”‚
โ”œโ”€โ”€ /src
โ”‚   โ”œโ”€โ”€ /configs
โ”‚   โ”‚   โ”œโ”€โ”€ index.ts
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ /controllers
โ”‚   โ”‚   โ”œโ”€โ”€ auth.controller.ts
โ”‚   โ”‚   โ”œโ”€โ”€ index.controller.ts
โ”‚   โ”‚   โ””โ”€โ”€ users.controller.ts
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ /dtos
โ”‚   โ”‚   โ””โ”€โ”€ users.dto.ts
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ /exceptions
โ”‚   โ”‚   โ””โ”€โ”€ HttpException.ts
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ /http
โ”‚   โ”‚   โ”œโ”€โ”€ auth.http
โ”‚   โ”‚   โ””โ”€โ”€ users.http
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ /interfaces
โ”‚   โ”‚   โ”œโ”€โ”€ auth.interface.ts
โ”‚   โ”‚   โ”œโ”€โ”€ routes.interface.ts
โ”‚   โ”‚   โ””โ”€โ”€ users.interface.ts
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ /middlewares
โ”‚   โ”‚   โ”œโ”€โ”€ auth.middleware.ts
โ”‚   โ”‚   โ”œโ”€โ”€ error.middleware.ts
โ”‚   โ”‚   โ””โ”€โ”€ validation.middleware.ts
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ /models
โ”‚   โ”‚   โ””โ”€โ”€ users.model.ts
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ /routes
โ”‚   โ”‚   โ”œโ”€โ”€ auth.route.ts
โ”‚   โ”‚   โ”œโ”€โ”€ index.route.ts
โ”‚   โ”‚   โ””โ”€โ”€ users.route.ts
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ /services
โ”‚   โ”‚   โ”œโ”€โ”€ auth.service.ts
โ”‚   โ”‚   โ””โ”€โ”€ users.service.ts
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ /tests
โ”‚   โ”‚   โ”œโ”€โ”€ auth.test.ts
โ”‚   โ”‚   โ”œโ”€โ”€ index.test.ts
โ”‚   โ”‚   โ””โ”€โ”€ users.test.ts
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ /utils
โ”‚   โ”‚   โ”œโ”€โ”€ logger.ts
โ”‚   โ”‚   โ”œโ”€โ”€ util.ts
โ”‚   โ”‚   โ””โ”€โ”€ vaildateEnv.ts
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ app.ts
โ”‚   โ”œโ”€โ”€ index.ts
โ”‚   โ””โ”€โ”€ server.ts
โ”‚
โ”œโ”€โ”€ .dockerignore
โ”œโ”€โ”€ .editorconfig
โ”œโ”€โ”€ .env.development.local
โ”œโ”€โ”€ .env.production.local
โ”œโ”€โ”€ .env.test.local
โ”œโ”€โ”€ .eslintignore
โ”œโ”€โ”€ .eslintrc
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ .huskyrc
โ”œโ”€โ”€ .lintstagedrc.json
โ”œโ”€โ”€ .prettierrc
โ”œโ”€โ”€ .swcrc
โ”œโ”€โ”€ docker-compose.yml
โ”œโ”€โ”€ Dockerfile
โ”œโ”€โ”€ ecosystem.config.js
โ”œโ”€โ”€ jest.config.js
โ”œโ”€โ”€ Makefile
โ”œโ”€โ”€ nginx.conf
โ”œโ”€โ”€ nodemon.json
โ”œโ”€โ”€ package-lock.json
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ swagger.yaml
โ””โ”€โ”€ tsconfig.json

Translations - read in your own language

Author: ljlm0402
Source Code: https://github.com/ljlm0402/typescript-express-starter 
License: MIT License

#typescript #node #express 

Alfie Mellor

Alfie Mellor

1594996008

Using TypeScript with Node.js and Express

Learn how to set up TypeScript in an Express.js app. This post describes a beginner-friendly way to set up TypeScript in an Express.js app and understand the basic constraints that come with it.

Ease of development is great when you are building a server written in JavaScript using Node.js and Express. What happens when this application server scales or you are working in a team of developers all across the world? TypeScript can help.

In this post, I am not going to talk about whether you should use TypeScript or not. This post describes a beginner-friendly way to set up TypeScript in an Express.js app and understand the basic constraints that come with it.

Prerequisites

To take full advantage of this tutorial, please make sure you have the following installed in your local development environment:

  • Node.js version >= 12.x.x installed
  • Access to one package manager such as npm or yarn
  • Basic knowledge of Node.js and Express

Create a minimal server with Express

Start by creating a new directory where you keep your side projects in your local development environment. Inside that directory, use npmโ€™s initializer command to create a package.json file:

media server
cd server/
npm init --yes

The --yes flag uses the default settings when initializing a package.json from npm config you might have set up.

The package.json file created might look something like this:

{
  "name": "express-ts-example",
  "version": "0.0.1",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "Aman Mittal (https://amanhimself.dev)",
  "license": "MIT"
}

After the initializing step, let us add an express package. From the terminal window, run the command:

yarn add express@4.17.1

Next, create a new file called index.js at the root of the project with the following code to trigger a minimal server:

const express = require('express');
const app = express();
const PORT = 8000;
app.get('/', (req,res) => res.send('Express + TypeScript Server'));
app.listen(PORT, () => {
  console.log(`โšก๏ธ[server]: Server is running at https://localhost:${PORT}`);
});

Go back to the terminal and trigger the common node index.js to start the server.

localhost displaying Express + TypeScript server

The Express server is now up and running.

Add TypeScript

Letโ€™s add two libraries to the development server as devDependencies.

  • typescript is a core library that helps to compile the TypeScript code to valid JavaScript
  • ts-node is a utility library that helps to run a development server written using TypeScript directly from the terminal

To install them, from a terminal window run the following command:

yarn add -D typescript ts-node

The -D flag is also known as --dev flag and is a specification for the package manager to install these libraries as devDependencies.

Once these libraries are installed, go to the package.json file and see a new devDependencies object:

"devDependencies": {
  "ts-node": "8.10.2",
  "typescript": "3.9.5"
}

Next, create a tsconfig.json file at the root of the development server project. This file allows you to customize TypeScript configuration and add other configurations to compile the TypeScript project:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "rootDir": "./",
    "outDir": "./build",
    "esModuleInterop": true,
    "strict": true
  }
}

The compilerOptions is a mandatory field that needs to be specified. The options used in the config above are:

  • target allows us to specify the target JavaScript version that compiler will output
  • module allows us to use a module manager in the compiled JavaScript code. The commonjs is supported and is a standard in Node.js
  • rootDir is an option that specifies where the TypeScript files are located inside the Node.js project
  • outDir specifies where the output of the compiled is going to be located
  • esModuleInterop allows us to compile ES6 modules to commonjs modules
  • strict is an option that enables strict type-checking options

There might be other configuration options that you can add on for the TypeScript compiler but these are the basic configuration options specified that can help you to get started.

#typescript #node #express #javascript #developer