{"id":26254299,"url":"https://github.com/danishzulfiqar/web-deploy-ref","last_synced_at":"2026-04-07T23:31:22.734Z","repository":{"id":193661455,"uuid":"526577342","full_name":"danishzulfiqar/Web-Deploy-Ref","owner":"danishzulfiqar","description":"Some references to HTML , CSS and Javascript","archived":false,"fork":false,"pushed_at":"2022-10-12T16:58:11.000Z","size":341,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-13T18:20:49.277Z","etag":null,"topics":["animation-css","css","html","javascript","nodejs"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danishzulfiqar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2022-08-19T11:17:55.000Z","updated_at":"2022-12-27T20:05:48.000Z","dependencies_parsed_at":"2023-09-09T10:11:35.015Z","dependency_job_id":null,"html_url":"https://github.com/danishzulfiqar/Web-Deploy-Ref","commit_stats":null,"previous_names":["danishzulfiqar/web-deploy-ref"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/danishzulfiqar/Web-Deploy-Ref","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danishzulfiqar%2FWeb-Deploy-Ref","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danishzulfiqar%2FWeb-Deploy-Ref/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danishzulfiqar%2FWeb-Deploy-Ref/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danishzulfiqar%2FWeb-Deploy-Ref/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danishzulfiqar","download_url":"https://codeload.github.com/danishzulfiqar/Web-Deploy-Ref/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danishzulfiqar%2FWeb-Deploy-Ref/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31533823,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-07T16:28:08.000Z","status":"ssl_error","status_checked_at":"2026-04-07T16:28:06.951Z","response_time":105,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["animation-css","css","html","javascript","nodejs"],"created_at":"2025-03-13T18:20:12.530Z","updated_at":"2026-04-07T23:31:22.711Z","avatar_url":"https://github.com/danishzulfiqar.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Web-Deploy-Ref\n\n# Nodejs and Express Cheatsheet\n\n\n\nnodejs works on a single thread, non blocking I/O and it handles it in a loop\n\n\n\n## Nodejs is best to use:\n\n- Rest API\n\n- Microservices\n\n- Realtime applications\n\n- CRUD applications (blog/shopping cart)\n\n- Tools and Utilities (command line tools as an example)\n\n\n\nthere are core module like:\n\n- path\n\n- filesystem (fs)\n\n- http\n\n\n\nto use them, you will call them in a variable (mostly they will be constants)\n\n```javascript\nconst path = require('path');\n```\n\nyou can also import functions that are exported to other javascript files to use them\n\n```javascript\nconst myfile = require('./myfile');\n```\n\n\n\n## Starting a Nodejs project\n\nyou will start by creating a ```package.json``` file using:\n\n```javascript\nnpm init \n\nnpm init -y //this won't ask you questions to fill for the package.json file\n```\n\n\n\nif you are using a specific project on another device, you can install all of your dependencies by using \n\n```javascript\nnpm install\n```\n\nthere are also dev dependencies, these dependencies only work on development enviroment not on production\n\n\n\nto install a dependency as a dev dependency\n\n```javascript\nnpm install -D nodemon\n```\n\nif you want to use elements inside of another file, you can export it\n\n```javascript\nconst person = {\n    name: 'John Doe',\n    age: 30\n}\nmodule.exports = person;\n```\n\nthen you can import it in another file\n\n```javascript\nconst person = require('./person');\n```\n\nyou can also export classes and functions as well\n\n```javascript\nclass Person {\n    constructor(name, age){\n        this.name = name;\n        this.age = age;\n    }\n    greeting(){\n        console.log(`My name is ${this.name} and I am ${this.age}`);\n    }\n}\nmodule.exports = Person;\n```\n\nthen you can import it and use it\n\n```javascript\nconst Person = require('./person');\nconst person1 = new Person('John Doe', 30);\n\nperson1.greeting(); // My name is John Doe and I am 30\n```\n\nkeep in mind that nodejs doesn't support ES6 importing\n\n```javascript\nimport Person from './person'\n```\n\nyou have to use BabelJS or Typescript and see what is backwards compatible to ES5\n\n## Path Module\n\n```javascript\nconst path = require('path');\n```\n\nto get the name of the file (base filename)\n\n```javascript\npath.basename(__filename) //path.js\n```\n\nto get the directory name\n\n```javascript\npath.dirname(__filename) // /users/myPc/nodeFile/pathFile\n```\n\nto get the file extension\n\n```javascript\npath.extname(__filename) // .js\n```\n\nto create a path object\n\n```javascript\npath.parse(__filename)\n\n// it will generate this object\n{\n    root: '/',\n    dir: '/users/myPc/nodeFile/pathFile',\n    ext: '.js',\n    name: 'path'\n}\n```\n\nsince it is an object so you can use any part of it\n\n\n\nto concatenate paths\n\n```javascript\npath.join(__dirname, 'test', 'hello.html')\n\n// it will create a path like this:\n// /users/myPc/nodeFile/pathFile/test/hello.html\n```\n\n\n\n## File system Module (fs)\n\n```javascript\nconst fs = require('fs');\nconst path = require('path');\n```\n\nto create a folder\n\n```javascript\nfs.mkdir(path.join(__dirname, '/test'), {}, function(err){\n    if(err) throw err;\n    console.log('folder created....');\n})\n```\n\nkeep in mind that this action is asynchronous so other tasks might happen in the background\n\nthere is a synchronous version of this command\n\n\n\nto create and write to a file\n\n```javascript\nfs.writeFile(path.join(__dirname, '/test', 'hello.txt'), 'Hello world!', err =\u003e {\n    if (err) throw err;\n    console.log('File written to.....');\n})\n```\n\nyou can use arrow functions instead of regular functions \n\n\n\nto add to a file (you use it in the callback)\n\n```javascript\nfs.appendFile(path.join(__dirname, '/test','hello.txt'), 'I love Nodejs', err =\u003e {\n    if(err) throw err;\n});\n```\n\nto read from file\n\n```javascript\nfs.readFile(path.join(__dirname, '/test', 'hello,txt'), 'utf8', (err, data) =\u003e {\n    if(err) throw err;\n    console.log(data);\n});\n\n// Hello world! I love Nodejs\n```\n\nto rename a file\n\n```javascript\nfs.rename(path.join(__dirname, '/test', 'hello.txt'), path.join(__dirname, '/test', 'helloworld.txt'), err =\u003e {\n    if(err) throw err;\n    console.log('File renamed......');\n});\n```\n\n\n\n## Operating System Module (os)\n\n```javascript\nconst os = require('os');\n```\n\nto get the platform\n\n```javascript\nos.platform()\n\n// darwin for macOS and win32 for windows\n```\n\nto get CPU architecture\n\n```javascript\nos.arch()\n// x64\n```\n\nto get CPU core info\n\n```javascript\nos.cpu()\n\n// it will return a big object with how many cores and much more\n```\n\nto get free available memory\n\n```javascript\nos.freemem()\n\n// 122388480\n```\n\nto get total memory\n\n```javascript\nos.totalmem()\n\n//34359738368\n```\n\nto get the home directory\n\n```javascript\nos.homedir()\n\n// /users/myPc\n```\n\nto get uptime (time the system has been up)\n\n```javascript\nos.uptime()\n\n// 1305116 (in seconds)\n```\n\n\n\n## Url Module (url)\n\n```javascript\nconst url = require('url');\n```\n\nto create a url\n\n```javascript\nconst myUrl = new url(\"http://myepicwebsite.com/hello.html/?id=10\");\n```\n\nto serialize the url\n\n```javascript\nmyUrl.href\nmyUrl.toString()\n\n// http://myepicwebsite.com/hello.html/?id=10\n```\n\nto get the host\n\n```javascript\nmyUrl.host\n\n//myepicwebsite.com\n```\n\nto get the hostname (same as host but minus the port)\n\n```javascript\nmyUrl.hostname\n\n//myepicwebsite.com\n```\n\nto get the pathname\n\n```javascript\nmyUrl.pathname\n\n// /hello.html\n```\n\nto serialize a query\n\n```javascript\nmyUrl.search\n\n// ?id=10\n```\n\ngenerate a params object (basically queries in an array)\n\n```javascript\nmyUrl.searchParams\n\n// { 'id' =\u003e '10', 'status' =\u003e 'active'}\n```\n\nyou can add to that param by appending to it\n\n```javascript\nmyUrl.searchParams.append('abc', '123');\n\n// { 'id' =\u003e '10', 'status' =\u003e 'active', 'abc' =\u003e '123'}\n```\n\nyou can loop through the params\n\n```javascript\nmyUrl.searchParams.forEach((value,name) =\u003e \n    console.log(`${name}: ${value}`);\n);\n```\n\n\n\n## Event Emitter Module\n\n```javascript\nconst EventEmitter = require('events');\n```\n\nfirst create a class \n\n```javascript\nclass MyEmitter extends EventEmitter {}\n```\n\ninitialize an object\n\n```javascript\nconst theEmitter = new MyEmitter();\n```\n\ncreate an event listner\n\n```javascript\ntheEmitter.on('event', () =\u003e console.log('Event!'));\n```\n\ninitialize an event\n\n```javascript\ntheEmitter.emit('event');\n\n// Event!\n```\n\n\n\n# Express\n\n```javascript\nconst express = require('express');\n```\n\ninitialize express\n\n```javascript\nconst app = express();\n```\n\ncreate your endpoint/ route handler\n\n```javascript\napp.get('/', function(req,res){\n    res.send('Hello world!');\n});\n```\n\nlisten to a port\n\n```javascript\napp.listen(5000);\n```\n\n\n\nbasic route handling\n\n```javascript\napp.get('/', function(req,res){\n    // fetch from database\n    // load pages\n    // return JSON\n    // full access to request and response\n})\n```\n\n\n\nyou can set up a port variable that takes either the server's port or a port of your choice if server doesn't have a port defined to it\n\n```javascript\nconst PORT = process.env.PORT || 5000;\n\napp.listen(PORT);\n```\n\nyou can parse HTML in a response\n\n```javascript\napp.get('/', (req,res) =\u003e {\n    res.send('\u003ch1\u003e Hello world \u003c/h1\u003e');\n});\n```\n\nto send a file\n\n```javascript\nconst express = require('express');\nconst path = require('path');\n\nconst app = express();\n\napp.get('/', (req,res) =\u003e {\n    res.sendFile(path.join(__dirname, 'public', 'index.html'));\n});\n```\n\nin express, you can set up a static folder to host static content\n\n```javascript\napp.use(express.static(path.join(__dirname,'public')));\n```\n\n\n\nto return json\n\n```javascript\nconst members = {\n    // a object that contains users with id/name/email\n    // id: 1,\n    // name: 'John Doe',\n    // email: 'john@doe.com'\n}\napp.get('/api/members',(req,res) =\u003e {\n    res.json(members);\n});\n```\n\nit will stringify the object in json format\n\n\n\ncreating middleware (code that works behind the scene)\n\n```javascript\nconst logger = (req,res,next) =\u003e {\n    console.log(`${req.protocol}://${req.get('host')}${req.originalUrl}`);\n next();\n}\n```\n\nto use this middleware\n\n```javascript\napp.use(logger);\n```\n\nto use url parameters\n\n```javascript\napp.get('/api/members/:id', (req,res)=\u003e {\n    res.send(req.params.id);\n});\n```\n\nto get a single member from that member's object we created before\n\n```javascript\napp.get('/api/members/:id', (req,res) =\u003e {\n    const found = members.some(member =\u003e member.id === parseInt(req.params.id));\n    \n    if(found){\n        res.json(member.filter(member =\u003e {\n            member.id === parseInt(req.params.id)\n        }))\n    }\n    else {\n        res.status(400).json({msg: \"member not found\"});\n    }\n});\n```\n\nthe some function returns a true/false based on the condition if it is satisfied\n\nthe filter will return an object with the elements that satisfy its condition\n\nyou parse the request parameters to integer because by default it is a string\n\n\n\nif not found return an error json with status of 400 (bad request), majority of the requests are send with status 200 (good request) so just to make sure that it is an error you add the 400 status\n\n\n\nexpress routing (you don't have to include every single route in app.js so you can use a router)\n\n\n\nassume you created a folder called routes, inside of it a folder called api then a javascript file called member.js\n\n\n\nso it will look like this  \n\nroutes -\u003e api -\u003e members.js\n\n\n\ninside members.js\n\n```javascript\nconst express = require('express');\nconst router = express.Router();\nconst members = { \n     // that object we define previously\n};\n\nrouter.get('/'(req,res)=\u003e {\n    res.json(members);\n});\n\nmodule.exports = router;\n```\n\nto use this router, in app.js\n\n```javascript\napp.use('/api/members', require('./routes/api/members'));\n```\n\nif you call now /api/members with a get request, it will give you the members\n\n\n\nexample if a post request\n\n```javascript\nrouter.post('/', (req,res) =\u003e {\n    res.send(req.body);    \n});\n```\n\nthe body won't work because it is not parsed\n\nto parse it, you add these lines to app.js\n\n```javascript\napp.use(express.json());\napp.use(express.urlencoded({extended: false})); // for url encoding\n```\n\n\n\ncreate a member example:\n\n```javascript\nrouter.post('/', (req,res) =\u003e {\n    const newMember = {\n        id: req.body.id, // or you can use a package like uuid\n        name: req.body.name,\n        email: req.body.email,\n        status: 'active'        \n    };\n    if(!newMember.name || !newMember.email){\n        return res.status(400).json({msg: \"provide email or name\"});\n    }\n    members.push(newMember);\n    res.json(members);\n});\n```\n\n\n\nupdate a member\n\n```javascript\nrouter.put('/:id', (req,res) =\u003e {\n    const found = members.some(member =\u003e member.id === parseInt(req.params.id));\n    \n   if(found){\n       const updateMember = req.body;\n       members.forEach(member =\u003e {\n           if(member.id === parseInt(req.params.id)){\n               member.name = updateMember.name ? updateMember.name : member.name;\n               member.email = updateMember.email ? updateMember.email : member.email;\n               res.json({msg: \"updated\"});\n           }\n           else {\n               req.status(400).json({msg: \"didn't update, user not found\"});\n           }\n       })\n   }\n});\n```\n\ndelete a member\n\n```javascript\nrouter.delete('/:id', (req,res) =\u003e {\n    const found = members.some(member =\u003e member.id === parseInt(req.params.id));\n    \n    if(found){\n        members = members.filter(member =\u003e member.id !== parseInt(req.params.id));\n        res.json({msg: \"member deleted\"});\n    }\n    else {\n        res.status(400).json({msg: \"cannot delete, member not found\"});\n    }\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanishzulfiqar%2Fweb-deploy-ref","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanishzulfiqar%2Fweb-deploy-ref","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanishzulfiqar%2Fweb-deploy-ref/lists"}