Created
January 19, 2018 21:04
-
-
Save mrcampbell/ca1745d097146588e883035416104174 to your computer and use it in GitHub Desktop.
Lunch and Learn Node.js Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- ./views/index.ejs --> | |
<h2><%- title %></h2> | |
<p>Welcome to my website!</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
var app = express(); | |
app.set('view engine', 'ejs'); | |
var port = process.env.PORT || 8080; | |
var apiRouter = express.Router(); | |
var siteRouter = express.Router(); | |
siteRouter.get('/', (req, res) => { | |
res.render('index', { | |
title: 'Red Pepper Node' | |
}); | |
}); | |
apiRouter.get('/', (req, res) => { | |
res.status(399).json({message: "Almost finished!"}); | |
}); | |
apiRouter.get('/hello', (req, res) => { | |
res.send("Its me"); | |
}); | |
apiRouter.get('/hello/:name', (req, res) => { | |
const name = req.params.name; | |
res.send("Hey, " + name + "!"); | |
}); | |
apiRouter.post('/submit', (req, res) => { | |
res.send("Hey!"); | |
}); | |
apiRouter.get('/restricted', authenticate, (req, res) => { | |
console.log("Did this run?"); | |
realm.getinstance(config).then(realm => { | |
read(() => { | |
res.send("LALALA") | |
}) | |
}) | |
res.send("Hey! I know you!"); | |
}); | |
function authenticate(req, res, next) { | |
console.log("Authenticated!!"); | |
next(); | |
console.log("Did this run? (AUTH)"); | |
} | |
app.use('/', siteRouter); | |
app.use('/api', apiRouter); | |
app.listen(port); | |
console.log("App is running on port: " + port); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "lunchandlearn", | |
"version": "0.0.1", | |
"description": "this is a sample project", | |
"main": "index.js", | |
"dependencies": { | |
"ejs": "^2.5.7", | |
"express": "^4.16.2", | |
"nodemon": "^1.14.11" | |
}, | |
"devDependencies": {}, | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"watch": "nodemon index.js" | |
}, | |
"author": "Mike", | |
"license": "ISC" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment