https://github.com/nickoki/node_app_checklist
A tl;dr checklist to guide your workflow for your new Node App
https://github.com/nickoki/node_app_checklist
Last synced: 4 months ago
JSON representation
A tl;dr checklist to guide your workflow for your new Node App
- Host: GitHub
- URL: https://github.com/nickoki/node_app_checklist
- Owner: nickoki
- Created: 2016-11-04T14:20:38.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-11-10T17:07:15.000Z (over 8 years ago)
- Last Synced: 2024-08-13T07:03:33.338Z (8 months ago)
- Size: 6.84 KB
- Stars: 5
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- jimsghstars - nickoki/node_app_checklist - A tl;dr checklist to guide your workflow for your new Node App (Others)
README
# node_app_checklist
A tl;dr checklist to guide your workflow for your new Node App## Set Up NodeJS and Express
- Initialize a **new NodeJS application** using Node Package Manager
```
$ npm init -y
```- (`-y` accepts all default settings)
- Install **Express**
```
$ npm install --save express
```- _Note:_ `--save` auto adds module to `package.json`
- Create `index.js` in root directory
- Require Express module and set up app port:
```javascript
var express = require("express")
var app = express()app.listen(4000, () => {
console.log("app listening on port 4000")
})
```- Start server with `nodemon` which automates server reload on file change (server start traditionally done with `node index.js`)
- _to install nodemon_ enter `npm install nodemon` (install globally with `npm install -g nodemon`)
## MongoDB & Mongoose ODM
- Install **Mongoose**
```
$ npm install --save mongoose
```- In `db/connection.js`, set up **Schema** and connection to the db:
```javascript
var mongoose = require("mongoose")mongoose.Promise = global.Promise
var ExampleSchema = mongoose.Schema({
name: String,
})mongoose.model("Example", ExampleSchema)
mongoose.connect("mongodb://localhost/")
module.exports = mongoose
```- Handy MongoDB CLI Commands:
- _Note:_ be sure `$ mongod` is running in a separate terminal window
- `$ mongo` opens interface
- `> show dbs`
- `> use `
- `> show collections`
- `> db..find({})` to show all
- [More commands...](https://docs.mongodb.com/v3.2/reference/mongo-shell/)## Making the MEN app MEANer (Angular)
- In `index.js` display root file: `layout.html`
```javascript
app.get("/", function(req, res) {
res.sendFile(__dirname + "/layout.html")
})
```- Set up Express to accept **AJAX requests** (from Angular):
```javascript
app.use(parser.json({extended: true}))
```- CDN **Angular** from `layout.html` & add link to your Angular app
```html
```- Example Angular Factory and Controller + Express API Route:
```javascript
// app.js (Angular)
function ExampleFactoryFunction($resource) {
return $resource("/api/example/:name", {}, {
update: { method: "put" }
})
}function indexController(ExampleFactory) {
ExampleFactory.query()
.$promise
.then(examples => this.examples = examples)
}function showController($stateParams, ExampleFactory) {
this.example = Example.get({name: $stateParams.name})
this.update = function() {
this.example.$update({name: $stateParams.name})
}
}
``````javascript
// index.js (Express)
app.post("/api/examples/:name", function(req, res) {
Example.findOneAndUpdate({name: req.params.name}, req.body, {new: true}).then(example => {
res.json(example)
})
})
```- Example: [WhenPresident](https://github.com/ga-wdi-exercises/whenpresident/tree/angular-solution)
-----
### Other Helpful Modules
- `body-parser`
- `hbs` (handlebars)