This repository is a Application software with TypeScript, Nodejs, Express, and MySQL, this application contains an API REST created with TypeScript.
Using TypeScript, Nodejs, Express, MySQL, etc preferably.
Using MySQL preferably.
Using Postman or RestEasy to feed the api.
$ 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:
...
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;
...
...
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
}
...
...
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!"
});
}
...
...
export interface Post {
id?: string;
title: string;
description: string;
body: string;
picture: string;
state: boolean;
created_at: Date;
}
...
...
import {Router} from 'express';
import postCTRL from '../controllers/posts.controller';
const router = Router();
router.route('/')
.get(postCTRL.getPosts)
.post(postCTRL.createPost);
...
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.
The gem is available as open source under the terms of the MIT License.