{"id":19753911,"url":"https://github.com/trisha/node-readme","last_synced_at":"2026-06-11T11:31:44.870Z","repository":{"id":167392169,"uuid":"328019658","full_name":"trisha/node-readme","owner":"trisha","description":null,"archived":false,"fork":false,"pushed_at":"2021-01-26T23:58:23.000Z","size":23,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-28T08:56:46.685Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/trisha.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-01-08T21:48:24.000Z","updated_at":"2021-01-26T23:58:26.000Z","dependencies_parsed_at":null,"dependency_job_id":"e377446b-08eb-49b3-a7d1-6b3c43d98b8e","html_url":"https://github.com/trisha/node-readme","commit_stats":null,"previous_names":["trisha/node-readme"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/trisha/node-readme","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trisha%2Fnode-readme","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trisha%2Fnode-readme/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trisha%2Fnode-readme/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trisha%2Fnode-readme/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trisha","download_url":"https://codeload.github.com/trisha/node-readme/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trisha%2Fnode-readme/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34197393,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-11T02:00:06.485Z","response_time":57,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":[],"created_at":"2024-11-12T02:55:38.302Z","updated_at":"2026-06-11T11:31:44.865Z","avatar_url":"https://github.com/trisha.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# node-readme\nThe purpose of this readme is to document how to initialize a new Node project.\n\nUp until now, we've been coding frontend things using HTML, CSS, and JavaScript files.\n\nAs we start coding things in the backend, we start to use node, which will allow us to run things in our terminal, especially since we might not be doing things in a browser and have access to a Chrome developer console to see our print statements. \n\nThis readme will have a summary of commands, and then at the bottom have an explanation for the commands in each section.\n\n# Create a project\n`mkdir [folderName]` to create a folder.\n\n\n`echo \"node_modules \\n .env\" \u003e\u003e .gitignore` to not upload node_modules and .env to git.\n\n`touch .env` to create an .env file.\n\n`git init` to add git tracking.\n\n\n`npm init -y`\n\n`\"_comments\": \"Add comments here\"` to your package.json to add comments.\n\n`npm i express` to be able to create an app.\n\n# Summaries \n### Add node to an existing project folder\nMake sure you are within the correct folder in the terminal or the build-in VSCode terminal\n1. `npm init -y`\n2. In package.json, you can add: `\"_comments\": \"Add comments here\"`\n\n### Run index.js in terminal with node\n1. `node index.js`\n\n### Import your own module\n1. In your myModule.js file, add `package.exports` in front of any data objects you would like to be able to export.\n2. In your index.js file, import your myModule.js file by typing `const myModule = require('./myModule.js')`\n\n### Import Express\n1. `npm install express`\n2. In index.js, type:\n\n    `const express = require('express')`\n\n    `const app = express()`\n\n    Where 'express' and 'app' are arbitrary (but semantic!) names for constants.\n\n    `// http://localhost:3000/`\n\n    `app.listen(3000, () =\u003e console.log(\"Listening on port 3000!\"))`\n\n3. Check out the 'sampleExpressApp' for sample code for an app. \n\n# Explanations\n### How to add Node to an existing project folder\n1. In either terminal or the built-in VSCode terminal, navigate to inside the folder that you want to add node to. \n\n    *If you don't already have a folder, run `mkdir folderName` and `cd folderName`, OR clone an existing Github folder with `git clone urlOfRepository`.*\n2. Run either `npm init -y` to accept all the default settings, or run `npm init` and hit enter through each setting to use npm (node package manager) to add node to your folder. This will create a package.json file. \n    You can't add // comments or your file will break. Instead, you can add a `\"_comments\": \"Add comments here\"` key to your JSON object. \n3. Create an index.js file if you don't have one already: `touch index.js`\n\n4. To run your code, type `node index.js` to run your code in the terminal.\n\n### How to import your own modules that you create \n1. Create a file for your module if you don't have one already: `touch myModule.js`\n2. Within your myModule.js file, make sure to `package.exports` whatever you want to export. For example, if you would like to export a function named beBasic, you would denote it as such within your myModule.js file:\n\n    `module.exports.beBasic = () =\u003e {\n        return \"That's so fetch!\"\n    }`\n\n    If you wanted to export an array named myFaveFoods, you would denote it as such:\n\n    `module.exports.myFaveFoods = ['Pad thai', 'Boba', 'Ferrero Rocher', 'Krispy Kreme glazed donuts', 'In N Out cheeseburgers with extra grilled onions, extra toasted']`\n\n3. Next, you'll want to import your module into your index.js file. Within your index.js file, make sure the include the below line of code: \n\n    `const myModule = require('./myModule.js')`\n\n    where 'myModule' is the name of the variable you would like to use, and './myModule.js' is the file path of where your module is located.\n\n\n\n### How to install npm packages, such as one that allows you to automatically run your backend code in your terminal whenever you save (nodemon)\n1. Download the nodemon package globally if you don't have it already: `npm install -g nodemon`\n\n    You can view a list of all your globally downloaded packages by running `npm list -g`\n\n    To download it locally and not globally to your folder, run `npm install nodemon`\n\n    The `-g` flag represents global download.\n\nWhenever you download a package while in your folder, your package.json file will update with those packages, which are now listed as dependencies. This package.json file (created when we added node during `npm init`) keeps track of which dependencies anyone else would need to download if they were to download you code and wanted to successfully run it. I think that running `npm start` will make your computer go through this list and automatically install them locally?\n\nUpon downloading your first package while in your folder, not only will your package.json file be updated with dependencies, a package-lock.json file will also be created. This file stores more detailed information about the dependencies that you've installed.\n\nNote: If you have any existing global packages, those will not be automatically added to your package.json file unless you `npm install` it while in your folder with your package.json file.  \n\n### How to NOT upload all your downloaded packages / dependencies to git.\nWhen we download 3rd party apps (typically via npm install), we don't want to upload those to Github. So we can tell git what to ignore when making commits.  \n`touch .gitignore` To prevent certain uploads to github. Write the name of the folder we want to ignore inside it:  `node_modules`\nNo need to type anything else in that file save for node_modules (the name of the folder with your npm installs)\n\nWe can also add .env (environment) which helps us store our API keys and other hidden credentials when we download the dotenv npm package. \n`require('dotenv').config()`\n// Process is a specific method that came with the dotenv module.\n`const omdb_API_key = process.env.omdb_API_key`\n\n### Mastering markdown: \nhttps://guides.github.com/features/mastering-markdown/\n\n### SQL\nHave PSQL downloaded\nIn terminal, type psql\n\\list to display all databases\n\\connect [name of database]\n\\d or \\dt to display all tables\n\\d [TABLE NAME]\n\nSELECT * FROM [TABLE]\n\nA callback function is a function that is passed to another function as an argument\n\n\nWe're using a template called EJS\nEJS = embedded JavaScript, allows us to inject HTML into it\n\nEJS is good for safety because it can grab stuff from secure databases and inject it into our frontend JavaScript files, where variables are shown but not their values.\n\nTemplates are kind of like DOM manipulation but with when we want the page to display different information depending on who's viewing it/who's logged in.\n\nPartial = a chunk of EJS code that can be reused.\n\n`npm install` to download package dependencies if you're downloading someone else's code. \n\nunit-2/labs/express-personal-website\n`npm i express`\nexpress allows us to create an app that accepts backend objects\n\nejs (npm i ejs) allows us to inject javascript into our \napp.set('view engine', 'ejs) (Middleware)\nCreate a Views folder\n\ncode-alongs/love-it-or-leave-it \nWe can use controllers with esj to route our page paths. And instead of app.get we use router.get\n\n`const express = require('express')`\n\n`const router = express.Router()`\n\n`const db = require('./models')` if we'll be using our sequelize models\n\n`const fs = require('fs')` if we'll be editing any files\n\nrouter.get('/', (req, res) =\u003e {\n    // res.send(\"Dinosaurs page\")\n    res.render('dinosaurs/index.ejs') // Automatically goes inside views folder.\n})\n\n`module.exports = router` at bottom of the page\n\n\nin our index.js, type app.use('/dinosaurs, require('./controllers/dinosaurs.js'))\n\n\n\nnpm i express-esj-layouts allows us to use a file in our Views folder named 'layout.esj' as a template.\nconst layouts = require('express-ejs-layouts')\n\napp.use(layouts) \n\nFor my other views, route to my .ejs with app.get\n\nunit-2/labs/learn_axios\ninstalling axios (npm i axios) replaces fetch, it's basically an easier/alternative way to fetch.\n\n// Import axios after we've installed it with npm.\n// Axios replaces fetch. \nAxios only requires one .then and one .catch, whereas fetch requires two .then. \nAxios automatically gives us a JSON object, and to access the data in that JSON object, we invoke the key specifically named 'data'. \nthe result we get with axios we need to grab the key 'data' to get our json search results. \n\n// Using files as our source of data.\nIf we have a .json file we're using, we'll have to read and write to that file but also convert from JSON to JavaScript to modify it, and then JS JSON to write to it.\nlet dinosaurs = fs.ReadFileSync('dinosaurs.json')\nlet dinoData = JSON.parse(dinosaurs) to convert from JSON to JavaScript.\n fs.writeFileSync('dinosaurs.json', JSON.stringify(dinoData)) to convert from JS to JSON and overwrite the file contents of dinosaurs.json.\n\n\n\n// Fetch is frontend and axios is backend, so we use it for security and so no one else can see our API key.\n// To hide our API keys from Github, we'll create a hidden file to store our keys called .env for environment.\n\nnpm i dotenv allows us to hide values in .env (environment)\n\nNeed to import and configure dotenv in our index.js or server.js.\n`require('dotenv').config()`\n\n\nGET retrieves data, POST sends new data, PUT edits data in place.\nIf I have a form on a view, below is what the code might look like:\n\n```\u003cform action=\"/dinosaurs\" method=\"POST\"\u003e\n    \u003clabel for=\"dinosaurType\"\u003eName\u003c/label\u003e\n    \u003cinput id=\"dinosaurType\" type=\"text\" name=\"name\"\u003e\n\n    \u003clabel for=\"dinosaurName\"\u003eType\u003c/label\u003e\n    \u003cinput id=\"dinosaurName\" type=\"text\" name=\"type\"\u003e \n\u003c!-- name= \"\" is for the body or JSON object key name. --\u003e\n\n    \u003cinput type=\"submit\"\u003e \n\u003c/form\u003e\n```\n\naction= refers to my path/route/URL pattern and method= refers to my HTML method. The name= refers to the JSON object key name I will use with 'body', such as 'body.name' to access it in the backend.\n\nhttps://gawdiseattle.gitbook.io/wdi/05-node-express/00readme-1/00readme/01get-post\n\nPUT and POST and DELETE\nrequest can only come from a form, can't come from a URL.\nIn order to parse the information we're receiving from a form, we have to import body-parser:\n\n// Sets up body-parser for parsing form data (req.body, req.body.${name='title'})\napp.use(express.urlencoded({ extended: false }))\n(Middleware)\n\nneed methodoverride if we want to put or delete anything.\nconst methodOverride = require('method-override') \n\napp.use(methodOverride('_method')) // Needs to be placed above anything related to HTTP request objects, including body-parser middleware. \n\n\n\nSEQUELIZE\nORM = Object-Relational Mapping\nAllows us to manipulate JavaScript objects instead of directly with the database\n\nmodel = template for our data objects.\n\npg = postgres\n\nsequelize-cli allows us to write sequelize commands in our command line. We installed this globally\n\nOnce we're in our folder:\nnpm init \n\nnpm install sequelize pg in our folder\n// Sequelize allows us to interface with SQL using JavaScript. PG is the 'dialect' of SQL that we're using, we used presql earlier but now we're using pg.\n\nsequelize init (this creates a config.json file)\n\ncode . (while in folder)\nconfig.json file: \n- Delete username and password underneath 'development'\n- Change development: dialect to 'postgres'\n- Change development: database to 'userapp_development' (name of your database)\n- We deleted test and production and only have development since that's all we need right now.\n\ncreatedb userapp_development\nor even sequelize db:create (sequelize db is a sequelize command, create is a postgres command) and this will create a database named with the same name in our config.json file\n\npsql to enter presql\n\n\\list to list all databases\n\n`sequelize model:create --name user --attributes firstName:string,lastName:string,age:integer,email:string`\n^ Spacing is super important. Name our models as singular and our table names can be plural. 'String' has a char limit of 255 and 'text' doesn't.\nhttps://gawdiseattle.gitbook.io/wdi/05-node-express/express-sequelize/03setup\n\nCreates a new file in migrations folder (for migrating to SQL) and another in models folder (to see our model format)\n\n`sequelize db:migrate` // Automatically creates a table with the same name as the model but with an s at the end\nIf you mess up with your migration, you can undo the migration (unmigrate), edit the names of your columns in your models file, and re-migrate. Sequelize tracks what files have or haven't already been migrated, so if a model schema has already been migrated, any changes you make to it won't reflect. [How to undo a migration](https://gawdiseattle.gitbook.io/wdi/05-node-express/express-sequelize/05validationsmigrations): `sequelize db:migrate:undo`\n\n\nIn index.js: const db = require('./models')\n\n`node index.js` everytime we want to run our code/make modifications to our model/table.\n\n// Adding 1:many relationships:\nUpdate models \u003e .js files \u003e associate for both directions. \n\n`models.user.hasMany(models.pet)\nmodels.pet.belongsTo(models.user)`\n\n// Adding many:many relationships:\n`sequelize model:create --name petToy --attributes petId:integer,toyId:integer`\n\nUpdate models \u003e .js files \u003e associations for both directions (pet, toy), but DON'T need to for the join model (petToy) as well.\n\n`models.toy.belongsToMany(models.pet, {through: 'petToy'})`\n\n`models.pet.belongsToMany(models.toy, {through: 'petToy'})`\n\n\nUsing and then running a test file to test that you have your association: \n\n```const db = require('./models')\n\ndb.comment.create({\n  name: 'Paul Allen',\n  content: 'This is really neat! Thanks for posting.',\n  articleId: 1\n})\n.then(comment =\u003e {\n  console.log(comment.get())\n})\n```\n\nUsing and then running a test file to check that we can query comments off of an article:\n```const db = require('./models')\n\ndb.article.findOne({\n  where: { id: 1 },\n  include: [db.comment]\n}).then(foundArticle =\u003e {\n  // by using eager loading, the article model should have a comments key\n  console.log(article.comments)\n  ```\n\n\n\n\n\n# Authentication\nnpm i session so we can view our session information\nnpm i bcrypt so we can hash passwords\nnpm i passport so we can authenticate user logins\nnpm i passport-local as our strategy so we can authenticate using local username and password, vs. instead of Google or Facebook, etc.\n\n\n\n\n## Getting Started When Downloading Another Project From Github:\nInstructions taken from the [project organizer project](https://github.com/WDI-SEA/express-project-organizer).\n\nFork and clone this repository\nRun npm install to install dependencies from the existing package.json file\nUse nodemon to start your application\nSetup your database (this app already has one existing model)\nRun createdb project_organizer_development to create the database\nRun sequelize db:migrate to run migrations\nRun sequelize db:seed:all to populate the database with 4 projects from previous Seattle cohorts.\n\n\npostgres needs escaping for capitols \\d+ \"PetToys\"","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrisha%2Fnode-readme","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrisha%2Fnode-readme","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrisha%2Fnode-readme/lists"}