mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
Initial commit
This commit is contained in:
@@ -1,19 +0,0 @@
|
|||||||
import { ExpressConfig } from "./config";
|
|
||||||
import { PassportJWTConfig } from "./config/passport-jwt.config";
|
|
||||||
import { NestApplication } from "./../src/";
|
|
||||||
|
|
||||||
export class Application implements NestApplication {
|
|
||||||
|
|
||||||
constructor(private app) {
|
|
||||||
ExpressConfig.setupConfig(this.app);
|
|
||||||
PassportJWTConfig.setupConfig(this.app);
|
|
||||||
}
|
|
||||||
|
|
||||||
public start() {
|
|
||||||
console.log("star t");
|
|
||||||
this.app.listen(3030, () => {
|
|
||||||
console.log("Nest Application listen on port:", 3030);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
export class ApplicationConfig {
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
import { Application } from "express";
|
|
||||||
import * as bodyParser from "body-parser";
|
|
||||||
import * as logger from "morgan";
|
|
||||||
|
|
||||||
export class ExpressConfig {
|
|
||||||
|
|
||||||
static setupConfig(app: Application) {
|
|
||||||
app.use(logger("dev"));
|
|
||||||
app.use(bodyParser.json());
|
|
||||||
app.use(bodyParser.urlencoded({ extended: true }));
|
|
||||||
|
|
||||||
// Add headers
|
|
||||||
app.use(function (req, res, next) {
|
|
||||||
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200');
|
|
||||||
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
|
|
||||||
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
|
|
||||||
next();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
export * from "./app.config";
|
|
||||||
export * from "./express.config";
|
|
||||||
export * from "./passport-jwt.config";
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
import * as _ from "lodash";
|
|
||||||
import { Application } from "express";
|
|
||||||
import * as passport from "passport";
|
|
||||||
import { ExtractJwt, Strategy, StrategyOptions } from "passport-jwt";
|
|
||||||
|
|
||||||
var users = [
|
|
||||||
{
|
|
||||||
id: 1,
|
|
||||||
name: 'jonathanmh',
|
|
||||||
password: '%2yx4'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 2,
|
|
||||||
name: 'test',
|
|
||||||
password: 'test'
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
export class PassportJWTConfig {
|
|
||||||
static readonly secretKey = "XD";
|
|
||||||
|
|
||||||
static readonly jwtOptions: StrategyOptions = {
|
|
||||||
jwtFromRequest: ExtractJwt.fromAuthHeader(),
|
|
||||||
secretOrKey: PassportJWTConfig.secretKey,
|
|
||||||
};
|
|
||||||
|
|
||||||
static setupConfig(app: Application) {
|
|
||||||
this.init();
|
|
||||||
app.use(passport.initialize());
|
|
||||||
}
|
|
||||||
|
|
||||||
static init() {
|
|
||||||
|
|
||||||
var strategy = new Strategy(this.jwtOptions, (payload, next) => {
|
|
||||||
console.log('payload received', payload);
|
|
||||||
|
|
||||||
var user = users[_.findIndex(users, {id: payload.id})];
|
|
||||||
console.log(user);
|
|
||||||
next(null, user || false);
|
|
||||||
});
|
|
||||||
|
|
||||||
passport.use(strategy);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
import * as mongoose from "mongoose";
|
|
||||||
mongoose.connect("mongodb://localhost:27017/tracker", {
|
|
||||||
server: {
|
|
||||||
socketOptions: {
|
|
||||||
socketTimeoutMS: 0,
|
|
||||||
connectTimeoutMS: 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
export { mongoose as db };
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
import * as mongoose from "mongoose";
|
|
||||||
import { db } from "./db";
|
|
||||||
|
|
||||||
interface IUser extends mongoose.Document {
|
|
||||||
name: string;
|
|
||||||
heh: any;
|
|
||||||
}
|
|
||||||
|
|
||||||
type UserType = IUser & mongoose.Document;
|
|
||||||
|
|
||||||
export const User = db.model<UserType>('User', new mongoose.Schema({
|
|
||||||
name : {type : String, required : true},
|
|
||||||
}));
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
import { UsersModule } from "./users/users.module";
|
|
||||||
import { AuthModule } from "./auth/auth.module";
|
|
||||||
import { Module } from "./../../src/";
|
|
||||||
|
|
||||||
@Module({
|
|
||||||
modules: [
|
|
||||||
UsersModule,
|
|
||||||
AuthModule
|
|
||||||
]
|
|
||||||
})
|
|
||||||
export class ApplicationModule {
|
|
||||||
configure() {}
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
import { AuthRoute } from "./login.route";
|
|
||||||
import { SharedModule } from "../shared.module";
|
|
||||||
import { Module } from "./../../../src/";
|
|
||||||
|
|
||||||
@Module({
|
|
||||||
modules: [
|
|
||||||
SharedModule,
|
|
||||||
],
|
|
||||||
controllers: [
|
|
||||||
AuthRoute
|
|
||||||
],
|
|
||||||
components: [
|
|
||||||
]
|
|
||||||
})
|
|
||||||
export class AuthModule {
|
|
||||||
configure() {
|
|
||||||
console.log("auth configured");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
import * as _ from "lodash";
|
|
||||||
import * as jwt from "jsonwebtoken";
|
|
||||||
import { Request, Response, NextFunction } from "express";
|
|
||||||
import { PassportJWTConfig } from "../../config/passport-jwt.config";
|
|
||||||
import { Controller, RequestMethod, RequestMapping } from "./../../../src/";
|
|
||||||
|
|
||||||
var users = [
|
|
||||||
{
|
|
||||||
id: 1,
|
|
||||||
name: 'jonathanmh',
|
|
||||||
password: '%2yx4'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 2,
|
|
||||||
name: 'test',
|
|
||||||
password: 'test'
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
@Controller({ path: "login" })
|
|
||||||
export class AuthRoute {
|
|
||||||
|
|
||||||
constructor() {}
|
|
||||||
|
|
||||||
@RequestMapping({ path: "/", method: RequestMethod.POST })
|
|
||||||
fetchToken(req: Request, res: Response, next: NextFunction) {
|
|
||||||
const { name, password } = req.body;
|
|
||||||
|
|
||||||
const user = users[_.findIndex(users, {name: name})];
|
|
||||||
if (!user){
|
|
||||||
res.status(401).json({message:"no such user found"});
|
|
||||||
}
|
|
||||||
|
|
||||||
if(user.password === password) {
|
|
||||||
const payload = {id: user.id};
|
|
||||||
const token = jwt.sign(payload, PassportJWTConfig.secretKey);
|
|
||||||
res.json({message: "ok", token: token});
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
res.status(401).json({message:"passwords did not match"});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
|
|
||||||
import { SharedService } from "./shared.service";
|
|
||||||
import { Module } from "./../../src/";
|
|
||||||
|
|
||||||
@Module({
|
|
||||||
components: [
|
|
||||||
SharedService,
|
|
||||||
],
|
|
||||||
exports: [
|
|
||||||
SharedService,
|
|
||||||
]
|
|
||||||
})
|
|
||||||
export class SharedModule {
|
|
||||||
configure() {}
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
import { Component } from "./../../src/";
|
|
||||||
import { Subject } from "rxjs";
|
|
||||||
|
|
||||||
@Component()
|
|
||||||
export class SharedService {
|
|
||||||
public stream$ = new Subject<string>();
|
|
||||||
constructor() {
|
|
||||||
setTimeout(() => {
|
|
||||||
this.stream$.next("XDDD");
|
|
||||||
}, 3000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
//import { User } from "../../models/user";
|
|
||||||
import { SharedService } from "../shared.service";
|
|
||||||
import { Component, RequestMapping } from "./../../../src/";
|
|
||||||
import { UsersGateway } from "./users.gateway";
|
|
||||||
|
|
||||||
import { Subject } from "rxjs";
|
|
||||||
|
|
||||||
@Component()
|
|
||||||
export class UsersQueryService {
|
|
||||||
|
|
||||||
public stream$ = new Subject<string>();
|
|
||||||
static get dependencies() {
|
|
||||||
return [ UsersGateway, SharedService ];
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(private usersGateway: UsersGateway, private shared: SharedService) {
|
|
||||||
this.shared.stream$.subscribe((xd) => {
|
|
||||||
console.log("ONCE");
|
|
||||||
this.stream$.next(xd);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
getAllUsers(): Promise<any> {
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
User.find((err, res) => {
|
|
||||||
if(err) {
|
|
||||||
throw new Error(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve(res);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}*/
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
|
||||||
import { RequestMapping, RequestMethod} from "./../../../src/";
|
|
||||||
import { UsersQueryService } from "./users-query.service";
|
|
||||||
import { Controller } from "./../../../src/";
|
|
||||||
|
|
||||||
@Controller({ path: "users" })
|
|
||||||
export class UsersSecRoute {
|
|
||||||
|
|
||||||
constructor(private usersQueryService: UsersQueryService) {
|
|
||||||
this.usersQueryService.stream$.subscribe((xd) => {
|
|
||||||
console.log(xd);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/*@RequestMapping({
|
|
||||||
path: "/",
|
|
||||||
method: RequestMethod.GET
|
|
||||||
})
|
|
||||||
async getAllUsers(req: Request, res: Response, next: NextFunction) {
|
|
||||||
try {
|
|
||||||
const users = await this.usersQueryService.getAllUsers();
|
|
||||||
res.status(201).json(users);
|
|
||||||
next();
|
|
||||||
}
|
|
||||||
catch(e) {
|
|
||||||
next(e.message);
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
import { Gateway, SubscribeMessage, SocketServer, SocketGateway } from "./../../../src/socket";
|
|
||||||
import { UsersQueryService } from "./users-query.service";
|
|
||||||
import { Component } from "./../../../src/";
|
|
||||||
|
|
||||||
@Component()
|
|
||||||
@SocketGateway({ namespace: "" })
|
|
||||||
export class UsersGateway implements Gateway {
|
|
||||||
|
|
||||||
static get dependencies() {
|
|
||||||
return [ UsersQueryService ];
|
|
||||||
}
|
|
||||||
|
|
||||||
@SocketServer
|
|
||||||
private server;
|
|
||||||
|
|
||||||
constructor(private queryService: UsersQueryService) {
|
|
||||||
console.log("Gateway listen");
|
|
||||||
this.queryService.stream$.subscribe((xd) => {
|
|
||||||
console.log(xd);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
afterInit(server) {
|
|
||||||
this.server = server;
|
|
||||||
console.log("Server initialized");
|
|
||||||
}
|
|
||||||
|
|
||||||
handleConnection(client) {
|
|
||||||
console.log("Client connected");
|
|
||||||
setTimeout(() => {
|
|
||||||
client.emit("msg", { msg: "Hello from the server!" });
|
|
||||||
}, 2000);
|
|
||||||
/*
|
|
||||||
client.on("msg", (data) => {
|
|
||||||
console.log(data);
|
|
||||||
client.emit("msg", { msg: data.msg });
|
|
||||||
client.broadcast.emit("msg", { msg: data.msg });
|
|
||||||
});*/
|
|
||||||
}
|
|
||||||
|
|
||||||
handleDisconnect(client) {
|
|
||||||
console.log("Client disconnected ");
|
|
||||||
}
|
|
||||||
|
|
||||||
@SubscribeMessage({ value: "msg" })
|
|
||||||
msgHandler(client, data) {
|
|
||||||
console.log(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
import * as jwt from "jsonwebtoken";
|
|
||||||
import { UsersQueryService } from "./users-query.service";
|
|
||||||
import { Component, Middleware } from "./../../../src/";
|
|
||||||
|
|
||||||
@Component()
|
|
||||||
export class JWTMiddleware implements Middleware {
|
|
||||||
|
|
||||||
constructor(private usersQueryService: UsersQueryService) {}
|
|
||||||
|
|
||||||
resolve() {
|
|
||||||
return (req, res, next) => {
|
|
||||||
console.log("JWTmiddleware");
|
|
||||||
next();
|
|
||||||
/*var token = req.body.token || req.query.token || req.headers['x-access-token'];
|
|
||||||
|
|
||||||
if (token) {
|
|
||||||
|
|
||||||
// verifies secret and checks exp
|
|
||||||
jwt.verify(token, "XD", function(err, decoded) {
|
|
||||||
if (err) {
|
|
||||||
return res.json({ success: false, message: 'Failed to authenticate token.' });
|
|
||||||
} else {
|
|
||||||
// if everything is good, save to request for use in other routes
|
|
||||||
req.decoded = decoded;
|
|
||||||
next();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
// if there is no token
|
|
||||||
// return an error
|
|
||||||
return res.status(403).send({
|
|
||||||
success: false,
|
|
||||||
message: 'No token provided.'
|
|
||||||
});
|
|
||||||
|
|
||||||
}*/
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
|
|
||||||
import { UsersRoute } from "./users.route";
|
|
||||||
import { UsersQueryService } from "./users-query.service";
|
|
||||||
import { UsersGateway } from "./users.gateway";
|
|
||||||
import { UsersSecRoute } from "./users-sec.route";
|
|
||||||
import { JWTMiddleware } from "./users.middleware";
|
|
||||||
import { SharedModule } from "../shared.module";
|
|
||||||
import { SharedService } from "../shared.service";
|
|
||||||
import { Module } from "./../../../src/";
|
|
||||||
|
|
||||||
@Module({
|
|
||||||
modules: [
|
|
||||||
SharedModule,
|
|
||||||
],
|
|
||||||
controllers: [
|
|
||||||
UsersRoute,
|
|
||||||
UsersSecRoute
|
|
||||||
],
|
|
||||||
components: [
|
|
||||||
UsersQueryService,
|
|
||||||
UsersGateway,
|
|
||||||
]
|
|
||||||
})
|
|
||||||
export class UsersModule {
|
|
||||||
|
|
||||||
configure(router) {
|
|
||||||
router.use({
|
|
||||||
middlewares: [ JWTMiddleware ],
|
|
||||||
forRoutes: [ UsersRoute, UsersSecRoute ]
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
|
||||||
import { UsersQueryService } from "./users-query.service";
|
|
||||||
import { RequestMethod, Controller, Exception, RequestMapping } from "./../../../src/";
|
|
||||||
|
|
||||||
class NotFoundException extends Exception {
|
|
||||||
constructor(msg: string) {
|
|
||||||
super(`Not found ${msg}`, 401);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Controller({ path: "users" })
|
|
||||||
export class UsersRoute {
|
|
||||||
|
|
||||||
get dependencies() {
|
|
||||||
return [ UsersQueryService ];
|
|
||||||
}
|
|
||||||
constructor(
|
|
||||||
private usersQueryService: UsersQueryService) {
|
|
||||||
|
|
||||||
//console.log(usersQueryService);
|
|
||||||
this.usersQueryService.stream$.subscribe((xd) => {
|
|
||||||
///console.log(xd);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping({
|
|
||||||
path: "/",
|
|
||||||
method: RequestMethod.GET
|
|
||||||
})
|
|
||||||
getAllUsers(req: Request, res: Response, next: NextFunction) {
|
|
||||||
throw new NotFoundException("user");
|
|
||||||
/* try {
|
|
||||||
//const users = await this.usersQueryService.getAllUsers();
|
|
||||||
res.status(201).json({
|
|
||||||
data: [
|
|
||||||
{ name: "Todo 1", status: 3 },
|
|
||||||
{ name: "Todo 2", status: 0 },
|
|
||||||
{ name: "Todo 3", status: 2 },
|
|
||||||
],
|
|
||||||
});
|
|
||||||
next();
|
|
||||||
}
|
|
||||||
catch(e) {
|
|
||||||
next(e.message);
|
|
||||||
}*/
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping({
|
|
||||||
path: "/",
|
|
||||||
method: RequestMethod.POST
|
|
||||||
})
|
|
||||||
async addTodoItem(req: Request, res: Response) {
|
|
||||||
try {
|
|
||||||
console.log(req.body);
|
|
||||||
res.status(201).json({});
|
|
||||||
//const users = await this.usersQueryService.getAllUsers();
|
|
||||||
/*res.status(201).json({
|
|
||||||
data: [
|
|
||||||
{ name: "Todo 1", status: 3 },
|
|
||||||
{ name: "Todo 2", status: 0 },
|
|
||||||
{ name: "Todo 3", status: 2 },
|
|
||||||
],
|
|
||||||
});*/
|
|
||||||
}
|
|
||||||
catch(e) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
import { NestRunner } from "./../src/";
|
|
||||||
import { Application } from "./app";
|
|
||||||
import { ApplicationModule } from "./modules/app.module";
|
|
||||||
|
|
||||||
NestRunner.run(Application, ApplicationModule);
|
|
||||||
Reference in New Issue
Block a user