├── .gitignore ├── .vscode ├── launch.json └── tasks.json ├── README.md ├── app.ts ├── app ├── data │ └── data.json ├── routes │ └── crud.ts └── services │ └── userService.ts ├── package.json ├── public ├── index.html └── t.jpg └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | *.map 3 | *.js 4 | 5 | node_modules 6 | 7 | npm-debug.log 8 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "name": "Launch", 6 | "type": "node", 7 | "request": "launch", 8 | "program": "${workspaceRoot}/app.js", 9 | "stopOnEntry": false, 10 | "args": [], 11 | "cwd": "${workspaceRoot}", 12 | "preLaunchTask": null, 13 | "runtimeExecutable": null, 14 | "runtimeArgs": [ 15 | "--nolazy" 16 | ], 17 | "env": { 18 | "NODE_ENV": "development" 19 | }, 20 | "console": "internalConsole", 21 | "sourceMaps": true, 22 | "outFiles": [] 23 | }, 24 | { 25 | "name": "Attach", 26 | "type": "node", 27 | "request": "attach", 28 | "port": 5858, 29 | "address": "localhost", 30 | "restart": false, 31 | "sourceMaps": false, 32 | "outFiles": [], 33 | "localRoot": "${workspaceRoot}", 34 | "remoteRoot": null 35 | }, 36 | { 37 | "name": "Attach to Process", 38 | "type": "node", 39 | "request": "attach", 40 | "processId": "${command.PickProcess}", 41 | "port": 5858, 42 | "sourceMaps": false, 43 | "outFiles": [] 44 | } 45 | ] 46 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | // See https://go.microsoft.com/fwlink/?LinkId=733558 3 | // for the documentation about the tasks.json format 4 | "version": "0.1.0", 5 | "command": "tsc", 6 | "isShellCommand": true, 7 | "args": ["-p", "."], 8 | "showOutput": "silent", 9 | "problemMatcher": "$tsc" 10 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Nodejs-Mongo-Cassandra -------------------------------------------------------------------------------- /app.ts: -------------------------------------------------------------------------------- 1 | import * as express from "express"; 2 | import * as bodyParser from "body-parser"; 3 | import * as cookieParser from "cookie-parser"; 4 | import * as os from "os"; 5 | import * as us from "./app/services/userService"; 6 | 7 | var cluster = require('cluster'); 8 | 9 | if (cluster.isMaster) { 10 | var cpuCount = require('os').cpus().length; 11 | console.log(cpuCount); 12 | 13 | for (var i = 0; i < cpuCount; i++) { 14 | cluster.fork(); 15 | } 16 | 17 | cluster.on('exit', (worker: any, code: any, signal: any) => { 18 | console.log('Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal); 19 | console.log('Starting a new worker'); 20 | cluster.fork(); 21 | }); 22 | } 23 | else { 24 | var app = express(); 25 | var datas: { datas: Array<{ name: string }> } = require("./app/data/data.json"); 26 | 27 | app.set("datas", datas); 28 | 29 | app.use(express.static('public')); 30 | app.use(cookieParser()); 31 | app.use(bodyParser.json()); 32 | app.use(bodyParser.urlencoded({ extended: false })); //for post request 33 | 34 | app.use(require('./app/routes/crud')); 35 | app.get('/test1', (req, res) => { 36 | console.log("t1 : " + cluster.worker.id); 37 | for (let i = 0; i < 100000000000000; i++) 38 | { } 39 | res.end(`test - ` + cluster.worker.id); 40 | }); 41 | app.get('/test2', (req, res) => { 42 | let service = new us.userService() 43 | service.addMongoTest().then(() => { 44 | res.end(`test2 - ` + cluster.worker.id); 45 | }); 46 | }); 47 | 48 | var server = app.listen(8081, () => { 49 | var host = server.address().address 50 | var port = server.address().port 51 | console.log(`runs in port ${port}`); 52 | }) 53 | 54 | } -------------------------------------------------------------------------------- /app/data/data.json: -------------------------------------------------------------------------------- 1 | 2 | 3 | { 4 | "datas": [ 5 | {"name": "ali"}, 6 | {"name": "reza"}, 7 | {"name": "ahmad"} 8 | ] 9 | } -------------------------------------------------------------------------------- /app/routes/crud.ts: -------------------------------------------------------------------------------- 1 | import * as express from "express"; 2 | import * as us from "./../services/userService"; 3 | var router = express.Router(); 4 | var datas: { datas: Array<{ name: string }> } = require("./../data/data.json"); 5 | 6 | router.get('/hello', (req, res) => { 7 | let cookie = req.cookies; 8 | res.send('