Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 17 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,17 @@
# Node Initial Demo

### Project Structure

Main structure of node.js project. Folders / files:

- <b>\_\_tests__</b>. Tests folder. See [Jest Docs](https://jestjs.io/es-ES/docs/configuration) and [Chai Docs](https://www.chaijs.com/)
- <b>app</b>:
- <b>config</b>
- <b>controllers</b>
- <b>middlewares</b>
- <b>models</b>
- <b>routes</b>
- <b>helpers</b>
- <b>app.js</b>. Entry point.
- <b>package.json</b>.
- <b>.env</b>. Environment descriptor. See [dotenv doc](https://www.npmjs.com/package/dotenv).

Extras:
- <b>.eslintrc</b>. Linter JS, static code analyzer. See [EsLint Docs](https://eslint.org/docs/user-guide/configuring/configuration-files).
- <b>.prettierignore</b>. Code formatter. See [Prettier Config](https://prettier.io/docs/en/configuration.html) and [Prettier Ignore](https://prettier.io/docs/en/ignore.html).
- <b>.ecosystem.config.js</b>. Process Manage at runtime. See [PM2 Docs](https://pm2.keymetrics.io/).

### Import project for use with Visual Studio Code

Follow the steps below:
* Fork the project from https://github.com/IT-Academy-BCN/nodeInitialDemo
* Clone your fork of the project from the Github Platform. Execute:
```
git clone https://github.com/your_username_here/nodeInitialDemo
```
* Open the project downloaded
![Open Project](img/VSC_open.png)


### Import project for use with WebStorm

Follow the steps below:
* Fork the project from https://github.com/IT-Academy-BCN/nodeInitialDemo
* Clone your fork of the project from the Github Platform. Execute:
```
git clone https://github.com/your_username_here/nodeInitialDemo
```
* Open the project downloaded
![Open Project](img/webstorm_open.png)


### Utilities

* [Node Developers Guide](https://nodejs.dev/learn)
* **.gitignore file** configuration. See [Official Docs](https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files).
* **Git branches**. See [Official Docs](https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell)
# Entrega 4.1

## Requiere instalar dependencias:


## Funcionamiento

- npm i --> para instalar dependencias

- npm run --> start para ejecutar la aplicación

## POSTMAN COLLECTION

- importar archivo desde postman: entrega41.postman_collection.json



11 changes: 11 additions & 0 deletions __tests__/primerTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// import getPokemon from "../app/controllers/getPokemon";

// ///PASO 1
// describe("Test controlladores", () => {
// test("getPokemon", () => {
// const req: Request = {}
// const res:object = {}
// const result: object = {}
// expect(getPokemon(req, res)).toBe(result);
// })
// });
1 change: 0 additions & 1 deletion app/app.js

This file was deleted.

20 changes: 20 additions & 0 deletions app/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const express = require("express")
const app = express()
const fileUpload = require("express-fileupload")

import userRouter from "./routes/users"
import imgRouter from "./routes/imgRouter"
import timeRouter from "./routes/timeRouter"
import pokemonRouter from "./routes/pokemonRouter"

app.use(fileUpload())
app.use(express.json())
app.use(userRouter) // nivell 1 exercici 1
app.use(imgRouter) // nivell 1 exercici 2
app.use(timeRouter) /// NIVELL 2
app.use(pokemonRouter) // NIVEL 3

const port: string | number = process.env.PORT || 5000
app.listen(port, (): void => {
console.log(`Server iniciado en puerto ${port}`)
})
Empty file removed app/config/config.js
Empty file.
Empty file removed app/controllers/controller.js
Empty file.
18 changes: 18 additions & 0 deletions app/controllers/getPokemon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import axios from 'axios'
import {Request, Response} from 'express'

const getPokemon = async (req: Request, res: Response) => {
const id:number = parseInt(req.params.id)
const buscarPokemon = async (id: number): Promise<object> => {
let x = await axios(`https://pokeapi.co/api/v2/pokemon/${id}`).then(
(res: any) =>
(res = {
nom: res.data.species.name,
pes: res.data.weight,
})
)
return x
}
res.send(await buscarPokemon(id))
}
export default getPokemon
7 changes: 7 additions & 0 deletions app/controllers/getUsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {Request, Response} from 'express'

const getUsers = (req: Request, res: Response): void => {
res.send([{ name: "David", age: 37, pass: 123 }, `Request url: ${req.url}`])
}

export default getUsers
14 changes: 14 additions & 0 deletions app/controllers/postImg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const mymetypes = ["image/jpg", "image/png", "image/gif"]
const dirname = "./app/upload/"

const postImg = (req: any, res: any): Response => {
if (!req.files) {
return res.status(400).send("No file attached")
}
if (mymetypes.includes(req.files.imagen.mimetype)) {
req.files.imagen.mv(dirname + Date.now() + req.files.imagen.name)
return res.status(200).send("image uploaded")
} else return res.status(400).send("JPG/PNG/GIF Only")
}

export default postImg
7 changes: 7 additions & 0 deletions app/controllers/postTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {Request, Response} from 'express'
const postTime = (_req: Request, res: Response): void => {
const day = new Date()
res.status(200).send(day)
}

export default postTime
Empty file removed app/helpers/helper.js
Empty file.
File renamed without changes.
Binary file added app/img/mario.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file removed app/middlewares/middleware.js
Empty file.
12 changes: 12 additions & 0 deletions app/middlewares/nivell2exercici1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { NextFunction } from "connect"
import {Request, Response} from 'express'

const middle = (req: Request, res: Response, next: NextFunction) => {
if (!req.body.password) {
res.status(401).send('Req.body no contiene password')
}
if(req.body.password){next()}

}

export default middle
7 changes: 7 additions & 0 deletions app/middlewares/setAllowOrigin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { NextFunction } from 'connect'
import {Request, Response} from 'express'

export const setAllowOrigin = (_req: Request, res: Response, next: NextFunction): void=>{
res.set("Access-Control-Allow-Origin", "*")
next()
}
7 changes: 7 additions & 0 deletions app/middlewares/setCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { NextFunction } from 'connect'
import {Request, Response} from 'express'

export const setCache = (_req: Request, res: Response, next: NextFunction): void=>{
res.set("Cache-Control", "no-cache")
next()
}
Empty file removed app/models/model.js
Empty file.
8 changes: 8 additions & 0 deletions app/routes/imgRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import express from "express"
import postImg from "../controllers/postImg"

const router = express.Router()

router.post("/img", postImg)

export default router
8 changes: 8 additions & 0 deletions app/routes/pokemonRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import express from 'express'
import getPokemon from "../controllers/getPokemon"

const router = express.Router()

router.get("/pokemon/:id", getPokemon)

export default router
Empty file removed app/routes/route.js
Empty file.
14 changes: 14 additions & 0 deletions app/routes/timeRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import express from 'express'
import postTime from "../controllers/postTime"
import middle from '../middlewares/nivell2exercici1'
import { setCache } from '../middlewares/setCache'
import { setAllowOrigin } from '../middlewares/setAllowOrigin'

const router = express.Router()

router.use(setAllowOrigin)
router.use(setCache)
router.use(middle)
router.post("/time", postTime)

export default router
7 changes: 7 additions & 0 deletions app/routes/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import express from 'express'
import getUsers from '../controllers/getUsers'

const router = express.Router()
router.get("/", getUsers)

export default router
Binary file added app/upload/1679933221625mario.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/upload/1679936406963mario.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/upload/1679936421463mario.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/upload/1679936497474mario.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/upload/1679936563926mario.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/upload/1679936650310mario.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/upload/1679936676618mario.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/upload/1679936968605mario.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions build/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express = require("express");
const app = express();
const fileUpload = require("express-fileupload");
const users_1 = __importDefault(require("./routes/users"));
const imgRouter_1 = __importDefault(require("./routes/imgRouter"));
const timeRouter_1 = __importDefault(require("./routes/timeRouter"));
const pokemonRouter_1 = __importDefault(require("./routes/pokemonRouter"));
const nivell2exercici1_1 = __importDefault(require("./middlewares/nivell2exercici1"));
app.use(fileUpload());
app.use(express.json());
app.use(users_1.default); // nivell 1 exercici 1
app.use(imgRouter_1.default); // nivell 1 exercici 2
app.use(nivell2exercici1_1.default, timeRouter_1.default); /// NIVELL 2
app.use(pokemonRouter_1.default); // NIVEL 3
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Server iniciado en puerto ${port}`);
});
27 changes: 27 additions & 0 deletions build/controllers/getPokemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
const getPokemon = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const id = req.params.id;
const buscarPokemon = (id) => {
let x = axios_1.default(`https://pokeapi.co/api/v2/pokemon/${id}`).then((res) => (res = {
nom: res.data.species.name,
pes: res.data.weight,
}));
return x;
};
res.send(yield buscarPokemon(id));
});
exports.default = getPokemon;
6 changes: 6 additions & 0 deletions build/controllers/getUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const getUsers = (req, res) => {
res.send([{ name: "David", age: 37, pass: 123 }, `Request url: ${req.url}`]);
};
exports.default = getUsers;
16 changes: 16 additions & 0 deletions build/controllers/postImg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const mymetypes = ["image/jpg", "image/png", "image/gif"];
const dirname = "./app/upload/";
const postImg = (req, res) => {
if (!req.files) {
return res.status(400).send("No file attached");
}
if (mymetypes.includes(req.files.imagen.mimetype)) {
req.files.imagen.mv(dirname + Date.now() + req.files.imagen.name);
return res.status(200).send("image uploaded");
}
else
return res.status(400).send("JPG/PNG/GIF Only");
};
exports.default = postImg;
7 changes: 7 additions & 0 deletions build/controllers/postTime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const postTime = (_req, res) => {
const day = new Date();
res.send(day);
};
exports.default = postTime;
14 changes: 14 additions & 0 deletions build/middlewares/nivell2exercici1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const middle = (req, res, next) => {
res.set("Cache-Control", "no-cache");
res.header("Access-Control-Allow-Origin", "*");
req.body = { name: "David", age: 37, password: "asd" };
// console.log(req.body);
if (!req.body.password) {
res.status = 401;
// res.send(res.status);
}
next();
};
exports.default = middle;
10 changes: 10 additions & 0 deletions build/routes/imgRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const postImg_1 = __importDefault(require("../controllers/postImg"));
const router = express_1.default.Router();
router.post("/img", postImg_1.default);
exports.default = router;
10 changes: 10 additions & 0 deletions build/routes/pokemonRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const getPokemon_1 = __importDefault(require("../controllers/getPokemon"));
const router = express_1.default.Router();
router.get("/pokemon/:id", getPokemon_1.default);
exports.default = router;
10 changes: 10 additions & 0 deletions build/routes/timeRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const postTime_1 = __importDefault(require("../controllers/postTime"));
const router = express_1.default.Router();
router.post("/time", postTime_1.default);
exports.default = router;
10 changes: 10 additions & 0 deletions build/routes/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const getUsers_1 = __importDefault(require("../controllers/getUsers"));
const router = express_1.default.Router();
router.get("/", getUsers_1.default);
exports.default = router;
Loading