개발 => 복습 후 재정리 대기/Node.js

[Node.js][Express] 기본 틀 공부중

장 상 현 2021. 5. 25.

npm install express-generator -g

 

express -h

 

express myapp

 

npm start

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
app.get('', (req, res) => {
  res.sendFile(__dirname + '/index.html')
});
// GET method route
app.get('/', function (req, res) {
  res.send('GET request to the homepage');
});

// POST method route
app.post('/', function (req, res) {
  res.send('POST request to the homepage');
});
app.use(express.static('public'));
app.use(function(req, res, next) {
  res.status(404).send('Sorry cant find that!');
})

app.use(function(err, req, res, next) {
  console.error(err.stack);
  res.status(500).send('Something broke!');
});

댓글