Skip to content

DanielArturoAlejoAlvarez/api-rest-typescript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

API REST WITH TYPESCRIPT

Description

This repository is a Application software with TypeScript, Nodejs, Express, and MySQL, this application contains an API REST created with TypeScript.

Installation

Using TypeScript, Nodejs, Express, MySQL, etc preferably.

DataBase

Using MySQL preferably.

Apps

Using Postman or RestEasy to feed the api.

Usage

$ git clone https://github.com/DanielArturoAlejoAlvarez/api-rest-typescript.git [NAME APP] 

$ npm install

$ npm run dev (DEVELOPMENT)

$ npm run build (PRODUCTION)

Follow the following steps and you're good to go! Important:

alt text

Coding

DATABASE

...
CREATE DATABASE api_rest_typescript_db;

CREATE TABLE posts(
    id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(128) NOT NULL, 
    description VARCHAR(256) NOT NULL,
    body TEXT NOT NULL,
    picture VARCHAR(512),
    state BOOLEAN DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

DESCRIBE post;
...

Config

...
export const Keys = {
    host: process.env.HOST || '127.0.0.1',
    user: process.env.SUPERUSER || 'root',
    password: process.env.PASS || '',
    database: process.env.DB_NAME || 'api_rest_typescript_db',
    connectionLimit: 10 
}
...

Controllers

...
const getPosts = async (req: Request,res: Response): Promise<Response> => {
    const conn = await connect();
    const posts = await conn.query("SELECT * FROM posts");
    return res.json(posts[0])
}

const createPost = async (req: Request,res: Response) => {
    const newPost: Post = req.body;
    console.log(newPost);
    const conn = await connect();
    await conn.query("INSERT INTO posts SET ?", [newPost]);
    return res.json({
        msg: "Post saved successfully!"
    });
}
...

Models

... 
export interface Post {
    id?: string;
    title: string;
    description: string;
    body: string;
    picture: string;
    state: boolean;
    created_at: Date;
}
...

Routes

... 
import {Router} from 'express';
import postCTRL from '../controllers/posts.controller';
const router = Router();
router.route('/')
    .get(postCTRL.getPosts)
    .post(postCTRL.createPost);
...

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/DanielArturoAlejoAlvarez/api-rest-typescript. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

About

Software of Development with Typescript and MySQL (REST API)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages