본문으로 바로가기

 

node에도 spring initialazer와 같은 기능이 있는 것 같다.

 

sudo npm install -g express-generator

해당 쉘을 실행하면 비밀번호를 물어본다 -g를 붙여 관리자모드를 하였으므로

이후

express HelloExpress

라고 실행하면

 

 

bagtaejun-maegbugpeulo:node2 tjpark$ express HelloExpress

  warning: the default view engine will not be jade in future releases
  warning: use `--view=jade' or `--help' for additional options


   create : HelloExpress/
   create : HelloExpress/public/
   create : HelloExpress/public/javascripts/
   create : HelloExpress/public/images/
   create : HelloExpress/public/stylesheets/
   create : HelloExpress/public/stylesheets/style.css
   create : HelloExpress/routes/
   create : HelloExpress/routes/index.js
   create : HelloExpress/routes/users.js
   create : HelloExpress/views/
   create : HelloExpress/views/error.jade
   create : HelloExpress/views/index.jade
   create : HelloExpress/views/layout.jade
   create : HelloExpress/app.js
   create : HelloExpress/package.json
   create : HelloExpress/bin/
   create : HelloExpress/bin/www

   change directory:
     $ cd HelloExpress

   install dependencies:
     $ npm install

   run the app:
     $ DEBUG=helloexpress:* npm start

 

 

현재경로에서 새로운 프로젝트가 생성된다.

 

package.json을 보면

{
  "name": "helloexpress",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "cookie-parser": "~1.4.4",
    "debug": "~2.6.9",
    "express": "~4.16.1",
    "http-errors": "~1.6.3",
    "jade": "~1.11.0",
    "morgan": "~1.9.1"
  }
}

 

./bin/www

#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
var debug = require('debug')('helloexpress:server');
var http = require('http');

/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

/**
 * Event listener for HTTP server "listening" event.
 */

function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
}
DEBUG=HelloExpress:* npm start
--> DEBUG라는 환경변수를 설정하는 것이다.
echo %DEBUG%로 출력된다.
--> 현재 세션을 종료하면 초기화됨.