├── .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('

Hello World

'); 9 | }); 10 | 11 | // router.use((req, res, next) => { 12 | // console.log(req.url); 13 | // next(); 14 | // }); 15 | 16 | router.get('/getData', async (req, res) => { 17 | let response1 = datas.datas.filter(c => c.name.toLowerCase() == "ali"); 18 | let response2: { datas: Array<{ name: string }> } = req.app.get("datas"); 19 | let service = new us.userService(); 20 | let response3 = await service.getAllUser(true); 21 | res.end(JSON.stringify(response3)); 22 | }); 23 | 24 | router.get('/get/:id', (req, res) => { 25 | let id = req.params.id; 26 | let response = { 27 | FirstName: req.query.FirstName, 28 | LastName: req.query.LastName, 29 | Id: id 30 | }; 31 | res.end(JSON.stringify(response)); 32 | 33 | //res.send('html'); with content-type: text/html 34 | }); 35 | 36 | router.post('/post', function (req, res) { 37 | let response = { 38 | FirstName: req.body.FirstName, 39 | LastName: req.body.LastName 40 | }; 41 | res.end(JSON.stringify(response)); 42 | }); 43 | 44 | module.exports = router; -------------------------------------------------------------------------------- /app/services/userService.ts: -------------------------------------------------------------------------------- 1 | var MongoClient = require('mongodb').MongoClient; 2 | 3 | export class userService { 4 | public async getAllUser(isValid: boolean): Promise { 5 | let num = await this.test(); 6 | return num; 7 | } 8 | 9 | private async test(): Promise { 10 | return new Promise((res) => { 11 | let num = 1000; 12 | let i = 0; 13 | for (i = 0; i < num; i++) { 14 | i++; 15 | //this.test(); 16 | } 17 | res(i); 18 | }); 19 | } 20 | 21 | public async addMongoTest(): Promise { 22 | let db = await MongoClient.connect("mongodb://localhost:27017/myDb"); 23 | 24 | let collection = await db.collection('Persons'); 25 | collection.insert({ id: 1, firstName: 'Steve', lastName: 'Jobs' }); 26 | collection.insert({ id: 2, firstName: 'Bill', lastName: 'Gates' }); 27 | collection.insert({ id: 3, firstName: 'James', lastName: 'Bond' }); 28 | 29 | } 30 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nodejs", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "@types/body-parser": "0.0.33", 13 | "@types/cookie-parser": "^1.3.30", 14 | "@types/express": "^4.0.34", 15 | "@types/multer": "0.0.32", 16 | "@types/node": "0.0.0", 17 | "body-parser": "^1.15.2", 18 | "cluster": "^0.7.7", 19 | "cookie-parser": "^1.4.3", 20 | "mongodb": "^2.2.13", 21 | "multer": "^1.2.0" 22 | }, 23 | "devDependencies": { 24 | "express": "^4.14.0", 25 | "nodemon": "^1.11.0" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 |

2 | Hello World

3 | 4 |
5 | First Name:
6 | Last Name: 7 | 8 |
-------------------------------------------------------------------------------- /public/t.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/darius-khll/Nodejs-Mongo-Cassandra/e17621edbe02aceca51555e83f26ca7909419670/public/t.jpg -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "noImplicitAny": true, 5 | "removeComments": false, 6 | "preserveConstEnums": true, 7 | "sourceMap": true, 8 | "target": "es6", 9 | "moduleResolution": "node", 10 | "watch": true 11 | }, 12 | "compileOnSave": true 13 | } --------------------------------------------------------------------------------