Class 20170126
Transcript of Class 20170126
-
2017.01.26 EXPRESSJS & TESTING
-
Ivan Wei - - -
-
cmder ( Open source )
http://cmder.net/
-
BASH ON UBUNTU ON WINDOWS
-
BASH ON UBUNTU ON WINDOWS
Windows 10 ( 64-bit )
OS build 14393 or later
()
https://msdn.microsoft.com/en-us/commandline/wsl/install_guide
-
NODE FRAMEWORKS
-
Express
koa.js
hapi
ThinkJS
Nodal
-
Feathers
ItemsAPI
KeystoneJS
Kraken
LEAN-STACK
LoopBack
MEAN
Sails
-
EXPRESS
-
NODE FRAMEWORKShttp://nodeframework.com/
-
HI, EXPRESS
-
1. npm install express-generator -g2. express --view=jade [myapp]3. cd myapp4. npm start
-
EXPRESS
-
git init
Git ( .git )
git branch git diff [file1 file2] git add files git commit new branch name git push
-
npm init ( ) package.json
npm start npm test ( npm run [script] )
-
app.jsconst express = require('express');
const app = express();
const http = require('http');
const port = 3000;
http.createServer(app)
.listen(port, function() {
console.log('App listening on port ' + port + '!'); // Server PORT
});
-
Node Servernode app.js
-
Controllers / Routers
controllers / routers
Router
-
index.jsconst express = require('express');
const router = express.Router();
router.get('/', function(req, res) {
res.end(It works);
});
module.exports = router ;
-
app.jsconst express = require('express');
const app = express();
const http = require('http');
const port = 3000;
const index = require(./controllers/index');
app.use('/', index);
http.createServer(app)
.listen(port, function() {
console.log('App listening on port ' + port + '!'); // Server PORT
});
-
Views
views
HTML / Jade (Pug) / EJS
-
index.html
Jade Jade - node template engine You are amazing Jade is a terse and simple templating language with a strong focus on performance and powerful features.
-
controllers/index.jsconst express = require('express');
const router = express.Router();
router.get('/', function(req, res) {
const options = {
root: './views/'
}
res.sendFile('index.html', options);
});
module.exports = router ;
-
HTML
-
Jade (Pug) / EJS /
-
Jade (Pug)
-
views/index.jadedoctype htmlhtml(lang='en') head title Jade body h1 Jade - node template engine (#{title}) #container.col p You are amazing p | Jade is a terse and simple | templating language with a | strong focus on performance | and powerful features.
-
app.jsconst express = require('express');
const app = express();
const http = require('http');
const port = 3000;
const index = require(./controllers/index');
app.use('/', index);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
http.createServer(app)
.listen(port, function() {
console.log('App listening on port ' + port + '!'); // Server PORT
});
-
controllers/index.jsconst express = require('express');
const router = express.Router();
router.get('/', function(req, res) {
res.render('index', { title: 'Express' });
});
module.exports = router ;
-
API
-
npm install should supertest --save-dev
npm install mocha -g
-
Mocha.js mocha.opts
Should.js
supertest http request
-
test/mocha.opts
--timeout 500000--require should--reporter spec--ui bdd--recursive ./app.js
-
CODE ()
https://github.com/IvanWei/Homework-20170126/blob/master/test/controller.spec.js
-
Q&A
-
https://github.com/IvanWei/Homework-20170126
2017/02/02
https://github.com/IvanWei/Homework-20170126