Coder Social home page Coder Social logo

info30005-w5-pre's Introduction

INFO30005 2019 workshop week 5

Express + Mongoose. Sample solution

Set up

Clone this repo.

git clone https://github.com/ZufengW/INFO30005-w5-pre.git

cd into the repo. Then install dependencies.

npm install

Running

Note that db.js uses the environment variable DB. You need to provide a value for this if you want the database to work.

Here is one way to run the app and provide an environment variable. (Replace YOUR_MONGODB_URI with your actual database connection string)

DB=YOUR_MONGODB_URI nodemon app

Then you can visit the website in your browser: http://localhost:3000/users/ (This is like sending GET requests)

Use a REST client such as Postman or Advanced REST Client to send http requests to your server.

Structure

INFO30005-w5-pre
├─┬ controllers
│ └── controller.js     Defines callback functions for routes
├─┬ models              Mongoose stuff
│ ├── db.js             Set up connection to database
│ └── users.js          User model
├── node_modules        Contains dependencies. Don't commit this file.
├─┬ routes              
│ └── users.js          Defines CRUD routes for users
├── .gitignore          Specify things you don't want version-controlled
├── app.js              Runs the app
├── package.json        Contains info about the project
├── package-lock.json   Contains more info about dependencies (don't edit this directly)
└── README.md           Contains info about the project for humans to read

Q and A

Why environment variables?

To avoid committing secrets such as API keys into your repo. You wouldn't want people to find out this info if you make your repo public.

What is () => {}?

It is an arrow function, which is a alternative to regular function expressions. It can be more compact than writing function.

Why const?

  • const is for declaring constants. Constants are block-scoped and can't be reassigned or redeclared. Use const wherever possible because it's the most restrictive.
  • let is likeconst, except they can be reassigned.
  • Avoid using var because the variable can be reassigned, redeclared and it is function scope.

How do you get a database connection string?

One possibility is http://mongodb.com/atlas, where you can get a free-tier cluster in the cloud.

Summary of steps:

  • Make an account
  • Build a new cluster
    • (e.g. AWS, Singapore, M0 tier)
  • Add a user to it with read and write access
  • Whitelist your IP addresses (or allow global access, which is easier)
  • Overview > Connect > Connect your application.
    • Copy the connection string.
    • Make sure to substitute in the user's password. The password needs to be URL encoded

It's also possible to get MongoDB running locally on your machine.

What does process.env.PORT || 3000 do?

|| is logical OR. If the left side (process.env.PORT) evaluates to something falsy (such as undefined), then the entire expression evaluates to the right side (3000).

Use this if you want to have a value with fallback to another value if the first value was undefined.

info30005-w5-pre's People

Contributors

zufengw avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.