Лекция 22
> heroku login
> node --version
> npm --version
> git --version
> git clone https://github.com/heroku/node-js-getting-started.git
> cd node-js-getting-started
> heroku create
> git push heroku master
> mkdir hello-world
> cd hello-world
{
"name": "hello-world",
"version": "1.0.0",
"scripts": {
"start": "node index.js"
}
}
> npm i --save express
'use strict';
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(port, function () {
console.log(`Server running on ${port}`);
});
.idea/
node_modules/
> git init
> git add *
> git commit -m 'init project'
> heroku create
> git push heroku master
> heroku open
'use strict';
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.send('Hello New World!');
});
app.listen(port, function () {
console.log(`Server running on ${port}`);
});
> git add *
> git commit -m 'changes'
> git push heroku master
> heroku open
> cd hello-world
> heroku addons:create heroku-postgresql:hobby-dev
> npm i --save sequelize pg pg-hstore
{
"db": {
"host": "ec2-54-243-185-99.compute-1.amazonaws.com",
"name": "da2f9fja1hin72",
"user": "hbbnkhapmikhuw",
"password": "95b2baec4326e5f3f25866addd55bad874570746a9f0f63040d87cdb02a40ad7",
"dialect": "postgres"
}
}
'use strict';
const express = require('express');
const Sequelize = require('sequelize');
const port = process.env.PORT || 3000;
const config = require('./config');
const dbcontext = require('./context/db')
(Sequelize, config);
const app = express();
...
...
app.get('/', (req, res) =>{...});
app.get('/generate', (req, res) => {...});
dbcontext.sequelize
.sync()
.then(() => {
app.listen(port, () => {
console.log(`Running on ${port} port`);
});
});
app.get('/', (req, res) =>{
dbcontext.rand
.findAll({ raw: true })
.then((rands) => res.json(rands));
});
app.get('/generate', (req, res) => {
dbcontext.rand
.create({
value: Math.random()
})
.then((rand) => res.json(rand));
});
'use strict';
module.exports = (Sequelize, config) => {
const options = { host: config.db.host,
dialect: config.db.dialect,
dialectOptions: { ssl: true } };
const sequelize = new Sequelize(config.db.name,
config.db.user, config.db.password, options);
const Rand = require('../models/rand')
(Sequelize, sequelize);
return { rand: Rand, sequelize: sequelize };
};
'use strict';
module.exports = (Sequelize, sequelize) => {
return sequelize.define('rands', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
value: Sequelize.INTEGER
});
};
> git add *
> git commit -m 'add database'
> git push heroku master
> heroku open
'use strict';
global.isProduction
= process.env.NODE_ENV === 'production';
{
"name": "hello-world",
"version": "1.0.0",
"dependencies": {
"express": "^4.14.0"
},
"scripts": {
"start": "node index.js"
},
"engines": {
"node": "6.9.4"
}
}
{
"name": "hello-world",
"version": "1.0.0",
"dependencies": {
"express": "^4.14.0"
},
"scripts": {
"start": "node index.js",
"heroku-prebuild": "echo This runs before Heroku installs your dependencies.",
"heroku-postbuild": "echo This runs afterwards."
}
}
const http = require('http');
const server = http.createServer((req, res) => {
res.end('Hello World\n');
});
server.listen(3000, '127.0.0.1', () => {
console.log('Running');
});
setTimeout(() => {
throw new Error('Surprise');
}, 5000);
> npm install pm2@latest -g
> pm2 start index.js
> pm2 stop index
> pm2 startup
> pm2 start index.js -i max
> pm2 reload index
> pm2 logs
> pm2 flush