$ npm init -y
$ npm install express --save
const express = require('express');
const bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.get('/query', (req, res) => {
let q = req.query.q;
res.send(`Got query ${q}`);
});
// POST http://localhost:8080/api/users
// parameters sent with
app.post('/api/users', (req, res) => {
var id = req.body.id;
var token = req.body.token;
var geo = req.body.geo;
res.send(user_id + ' ' + token + ' ' + geo);
});
app.listen(port, () => {
console.log(`Started on port ${port}`);
});
Use the express.static() middleware to specify directory containing the static files:
// file access at http://127.0.0.1:3000/style.css
app.use(express.static(__dirname + '/public'));
// file access at http://127.0.0.1:3000/public/style.css
app.use('/public', express.static(process.cwd() + '/public'));
Install hbs
$ npm install hbs --save
Then configure express to use handlebars:
const hbs = require('hbs');
app.set('view engine', 'hbs');
Create a views/
sub-directory (views/
is the default directory Express uses for templates)
$ mkdir views
Use res.render()
to inject variables into the hbs files:
res.render('about.hbs', {
pageTitle: 'About Page',
currentYear: new Date().getFullYear()
});
pageTitle
and currentYear
can then be used within about.hbs
:
<h1>{{ pageTitle }}</h1>
<p>Some text here</p>
<footer>
<p>Copyright {{ currentYear }}</p>
</footer>
To use partials, use registerPartials
to specify the directory containing the partials:
hbs.registerPartials(__dirname + '/views/partials');
Then, too include a partial (eg. /views/partials/footer.hbs
) within an hbs file:
{{> footer }}
Handlebars helpers can be used for common functions. For example:
hbs.registerHelper('getCurrentYear', () => {
return new Date().getFullYear();
});
hbs.registerHelper('screamIt', (text) => {
return text.toUpperCase();
});
and then in the templates you can use {{ getCurrentYear}}
or {{ screamIt 'upper case me' }}
, or {{ screamIt textVar }}
to run the functions.
Use app.use()
to register middleware. The middleware will be executed according to the order you call to app.use()
:
const fs = require('fs');
app.use((req, res, next) => {
var now = new Date().toString();
var log = `${now}: ${req.method} ${req.url}`;
fs.appendFile('server.log', log + '\n', (err) => {
if (err) {
console.log('Unable to append to server.log');
}
});
console.log(log);
next();
});
$ heroku help
$ heroku login
$ heroku keys:add
$ heroku keys
$ heroku keys:remove
$ ssh -v git@heroku.com
Set port
to process.env.PORT
in server.js to heroku can set the port for the application
using process.env.PORT
environment variable.
Setup a start
script in package.json for the Heroku to start the app:
{
...
"scripts": {
"start": "node server/server.js"
...
}
}
This is necessary because Heroku doesn't know the name of the file to use to start the app.
Run heroku create
from within the application:
$ heroku create
This will add a remote for Heroku:
$ git remote -vv
heroku https://git.heroku.com/hidden-scrubland-50797.git (fetch)
heroku https://git.heroku.com/hidden-scrubland-50797.git (push)
Now use git push
to push the application up to Heroku
$ git push heroku master
To open the application in your web browser use:
$ heroku open