Express 라이트 프레임

2260 단어
설치하다.
npm install express
HelloWord var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World!');}); var server = app.listen(3000, function () { var host = server.address().address; var port = server.address().port; console.log('Example app listening at http://%s:%s', host, port); });
간단한 Express 라우팅// "Hello World!" app.get('/', function (req, res) { res.send('Hello World!'); }); // POST app.post('/', function (req, res) { res.send('Got a POST request'); }); // /user PUT app.put('/user', function (req, res) { res.send('Got a PUT request at /user'); }); // /user DELETE app.delete('/user', function (req, res) { res.send('Got a DELETE request at /user'); });
Express 정적 파일 호스팅 Express express.static , 、CSS、JavaScript 。 express.static 。 , public 、CSS JavaScript , : app.use(express.static('public')); ,public 。 http://localhost:3000/images/kitten.jpg http://localhost:3000/css/style.css http://localhost:3000/js/app.js http://localhost:3000/images/bg.png http://localhost:3000/hello.html , , URL 。 , express.static : app.use(express.static('public')); app.use(express.static('files')); ,express.static 。 express.static “ (virtual)” ( ) , , : app.use('/static', express.static('public')); , “/static” public 。 http://localhost:3000/static/images/kitten.jpg http://localhost:3000/static/css/style.css http://localhost:3000/static/js/app.js http://localhost:3000/static/images/bg.png http://localhost:3000/static/hello.html
통합 MongodB : mongoskin $ npm install mongoskin var db = require('mongoskin').db('localhost:27017/animals'); db.collection('mamals').find().toArray(function(err, result) { if (err) throw err; console.log(result); });

좋은 웹페이지 즐겨찾기