├── .gitignore ├── modules ├── notes │ └── december.txt ├── cli.js ├── events.js ├── os.js ├── path.js └── fs.js ├── .DS_Store ├── package.json ├── templates ├── about.html ├── contact.html └── index.html └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules -------------------------------------------------------------------------------- /modules/notes/december.txt: -------------------------------------------------------------------------------- 1 | Create new course NodeJS and microservice project -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SameerBadriddinov/nodejs-asoslari/HEAD/.DS_Store -------------------------------------------------------------------------------- /modules/cli.js: -------------------------------------------------------------------------------- 1 | const logger = () => { 2 | const resp = {} 3 | 4 | for (let i = 2; i < process.argv.length; i++) { 5 | const arg = process.argv[i].split('=') 6 | resp[arg[0]] = arg[1] ? arg[1] : true 7 | } 8 | 9 | return resp 10 | } 11 | 12 | console.log(logger()) 13 | -------------------------------------------------------------------------------- /modules/events.js: -------------------------------------------------------------------------------- 1 | const Events = require('events') 2 | 3 | class Logger extends Events { 4 | log(a, b) { 5 | this.emit('calculate', a + b) 6 | } 7 | } 8 | 9 | const logger = new Logger() 10 | 11 | logger.on('calculate', data => { 12 | console.log(data) 13 | }) 14 | 15 | logger.log(8, 11) 16 | logger.log(7, 11) 17 | logger.log(6, 11) 18 | logger.log(5, 11) 19 | -------------------------------------------------------------------------------- /modules/os.js: -------------------------------------------------------------------------------- 1 | const os = require('os') 2 | 3 | // platform 4 | // console.log(os.platform()) 5 | 6 | // information 7 | // console.log(os.cpus()) 8 | 9 | // architecture 10 | // console.log(os.arch()) 11 | 12 | // free memory 13 | // console.log(os.freemem()) 14 | 15 | // total memory 16 | // console.log(os.totalmem()) 17 | 18 | // entry point 19 | console.log(os.homedir()) 20 | -------------------------------------------------------------------------------- /modules/path.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | 3 | // console.log(path.basename(__filename)) 4 | // console.log(path.dirname(__filename)) 5 | // console.log(path.extname(__filename)) 6 | 7 | // console.log(path.parse(__filename)) 8 | 9 | // console.log(path.join(__dirname, 'templates', 'index.html')) 10 | // console.log(path.resolve(__dirname, './templates', 'index.html')) 11 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "app", 3 | "version": "1.0.0", 4 | "description": "NodeJS starter lesson", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "node index.js", 8 | "dev": "nodemon index.js" 9 | }, 10 | "keywords": [ 11 | "nodejs" 12 | ], 13 | "author": "Samar Badriddinov", 14 | "license": "ISC", 15 | "devDependencies": { 16 | "nodemon": "^2.0.20" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /templates/about.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | About page 8 | 9 | 10 |

About page

11 |
12 |
13 | Go to main page 14 |
15 |
16 | Go to contact page 17 |
18 | 19 | 20 | -------------------------------------------------------------------------------- /templates/contact.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Conatct page 8 | 9 | 10 |

Conatct page

11 |
12 |
13 | Go to main page 14 |
15 |
16 | Go to about page 17 |
18 | 19 | 20 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Main page 8 | 9 | 10 |

Send name

11 |
12 | 13 | 14 |
15 |
16 |
17 | Go to about page 18 |
19 |
20 | Go to contact page 21 |
22 | Admin information 23 | 24 | 25 | -------------------------------------------------------------------------------- /modules/fs.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const path = require('path') 3 | 4 | // fs.mkdir(path.join(__dirname, 'templates'), err => { 5 | // if (err) throw new Error() 6 | 7 | // console.log('Folder was created successfully') 8 | // }) 9 | 10 | // fs.mkdir(path.join(__dirname, 'notes'), err => { 11 | // if (err) throw new Error() 12 | 13 | // console.log('Folder was created successfully') 14 | // }) 15 | 16 | fs.writeFile(path.join(__dirname, 'notes', 'december.txt'), 'Create new course NodeJS', err => { 17 | if (err) throw new Error() 18 | console.log('File was created successfully') 19 | 20 | fs.appendFile(path.join(__dirname, 'notes', 'december.txt'), ' and microservice project', err => { 21 | if (err) throw new Error() 22 | console.log('File was changed successfully') 23 | 24 | fs.readFile(path.join(__dirname, 'notes', 'december.txt'), 'utf-8', (err, data) => { 25 | if (err) throw new Error() 26 | console.log(data) 27 | }) 28 | }) 29 | }) 30 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const http = require('http') 2 | const fs = require('fs') 3 | const path = require('path') 4 | 5 | const server = http.createServer((req, res) => { 6 | if (req.method === 'GET') { 7 | res.writeHead(200, {'Content-Type': 'text/html'}) 8 | 9 | if (req.url === '/') { 10 | fs.readFile(path.join(__dirname, 'templates', 'index.html'), 'utf-8', (err, conten) => { 11 | if (err) throw err 12 | res.end(conten) 13 | }) 14 | } else if (req.url === '/about') { 15 | fs.readFile(path.join(__dirname, 'templates', 'about.html'), 'utf-8', (err, conten) => { 16 | if (err) throw err 17 | res.end(conten) 18 | }) 19 | } else if (req.url === '/contact') { 20 | fs.readFile(path.join(__dirname, 'templates', 'contact.html'), 'utf-8', (err, conten) => { 21 | if (err) throw err 22 | res.end(conten) 23 | }) 24 | } else if (req.url === '/api/admin') { 25 | res.writeHead(200, {'Content-Type': 'text/json'}) 26 | const admin = {name: 'Samar', surname: 'Badriddinov', job: 'Full-Stack developer'} 27 | res.end(JSON.stringify(admin)) 28 | } 29 | } else if (req.method === 'POST') { 30 | const body = [] 31 | res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'}) 32 | 33 | req.on('data', data => { 34 | body.push(Buffer.from(data)) 35 | }) 36 | 37 | req.on('end', () => { 38 | const message = body.toString().split('=')[1] 39 | 40 | res.end(`Name successfully added: ${message}`) 41 | }) 42 | } 43 | }) 44 | 45 | server.listen(3000, () => { 46 | console.log('Server has been started on port: 3000') 47 | }) 48 | --------------------------------------------------------------------------------