01-03 05:04
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[Web App/Node.js Express] UserId + HTML Tag 본문

Programming/Web App

[Web App/Node.js Express] UserId + HTML Tag

cinema4dr12 2014. 9. 8. 13:40

[웹 브라우저 창에서 UserID 받기]

app.js

var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var routes = require('./routes/index'); var users = require('./routes/users'); var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); app.set('port', process.env.PORT || 3000); // uncomment after placing your favicon in /public //app.use(favicon(__dirname + '/public/favicon.ico')); app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); app.use(bodyParser()); app.use('/', routes); app.use('hi', routes); app.get("/users/:userId", function(req, res){ res.send("<h1>Hello, user #" + req.params.userId); }); app.post("/users", function(req, res){ res.send("Creating a user with the name " + req.body.username + "."); }); // catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err); }); // error handlers // development error handler // will print stacktrace if (app.get('env') === 'development') { app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: err }); }); } // production error handler // no stacktraces leaked to user app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: {} }); }); var server = app.listen(app.get('port'), function() { console.log('Express server listening on port ' + server.address().port); }); module.exports = app;


[HTML 렌더링]

index.js

var express = require('express'); var router = express.Router(); /* GET home page. */ router.get('/', function(req, res) { res.render('index', { title: 'Express' }); }); router.get('/hi', function(req, res){ var message = [ "<h1>Hello Express</h1>", "<p>Welcome to My World</p>", "<p>You'll love my app</p>", "<ul>", "<li>", "Apple", "</li>", "<li>", "Banana", "</li>", "<li>", "Pineapple", "</li>", "<li>", "Peach", "</li>", "</ul>" ].join("\n"); res.send(message); }); module.exports = router;


Comments