JaxNode October 2017
4th Year Anniversary
Can the presenter build a express app in 2 lines of code?
// Basic example
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(3000);
const express = require('express');
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
const express = require('express');
const app = express();
const cookieParser = require('cookie-parser');
app.use(cookieParser());
const express = require('express');
const app = express();
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
const express = require('express');
const app = express();
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
const express = require('express');
const router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Hello World!' });
});
module.exports = router;
var express = require('express');
var router = express.Router();
// middleware that is specific to this router
router.use(function timeLog (req, res, next) {
console.log('Time: ', Date.now());
next();
});
// define the home page route
router.get('/', function (req, res) {
res.send('Birds home page');
});
// define the about route
router.get('/about', function (req, res) {
res.send('About birds');
});
module.exports = router;
extends layout
block content
mixin link(href, name)
a(href=href)= name
script(async='async', src='//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js')
ins.adsbygoogle(style='display: inline-block; width: 728px; height: 90px;', data-ad-client='ca-pub-3395496145928180', data-ad-slot='5814993232')
script.
(adsbygoogle = window.adsbygoogle || []).push({});
h1 Jax Upcoming Tech Events
p Here are upcoming events for people in the Jacksonville, Florida area interested in learning more about computer programming
| and computer languages and frameworks. Find your favorite developer and programming user groups on this site.
| Use this page to find programming and technology user group meetings.
div.row
div.col-md-3
a(href="https://itunes.apple.com/us/app/jax-tech-meetups/id919592266?mt=8", target="_blank")
img(src="images/appstore.png", alt="App Store")
div.col-md-9
p
em Please download our iPhone app. Android and Windows Phone coming soon.
{{!< LAYOUT}}
<h1>{{title}}</h1>
<p>
If you are interested in coming to our meetings or contacting our organizers,
please go to our <a href='http://meetup.com/Jax-Node-js-UG/'>Meetup page.</a>
</p>
<p>
You can also follow us on Twitter and GitHub!
</p>
<a href="https://twitter.com/jaxnode" class="twitter-follow-button" data-show-count="false" data-size="large">Follow @jaxnode</a>
<a class="github-button" href="https://github.com/jaxnode" data-style="mega" data-count-href="/jaxnode/followers" data-count-api="/users/jaxnode#followers" data-count-aria-label="# followers on GitHub" aria-label="Follow @jaxnode on GitHub">Follow @jaxnode</a>
// app.js
// Setup view engine
app.set('view engine', 'pug');
// setup route
app.get('/', function (req, res) {
res.render('index', { title: 'Hey', message: 'Hello there!' })
});
// index.pug
html
head
title= title
body
h1= message
const service = require('./services/mainService');
const routes = require('./routes/index');
const exposeService = function (req, resp, next) {
req.service = service;
next();
};
app.use('/', exposeService, routes);
const request = require('supertest');
const express = require('express');
const routesForApis = require('../routes/apiroutes');
const mockService = require('../services/mockService');
const app = express();
describe('API Routes', function () {
before(function () {
var exposeService = function (req, resp, next) {
req.service = mockService;
next();
};
app.use('/v1/api', exposeService, routesForApis);
});
describe('GET Meeting', function () {
it('responds to /v1/api/meeting', function testApi(done) {
request(app)
.get('/v1/api/meeting')
.expect('Content-Type', /application\/json/)
.expect(200, done);
});
});
});
const express = require('express')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
app.prepare()
.then(() => {
const server = express()
server.get('/p/:id', (req, res) => {
const actualPage = '/post'
const queryParams = { id: req.params.id }
app.render(req, res, actualPage, queryParams)
})
server.get('*', (req, res) => {
return handle(req, res)
})
server.listen(3000, (err) => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
})
.catch((ex) => {
console.error(ex.stack)
process.exit(1)
})
export default ({ color }) => (
<div>
Hello there <span>my friend</span>
<style jsx>{`
/* this style only applies to the span within lexical scope */
span { color: ${color}; }
`}</style>
</div>
)