Video address v.youku.com/v_show/id_X…

Git, gogs node, koa2 MongoDB ps: Since koA2 has started using async/await syntax, please ensure that the Node environment is above version 7.6.

Recently, WE are going to do a little interactive game to shake, originally wanted to use PHP to do, but too lazy configuration file, but laravel is also relatively large, want to simple processing, so WE plan to node simple processing gogs installation here will not be detailed, specific installation can go to the Gogs official website. Gogs. IO /docs/instal… Now it seems you have to climb a wall to access?? Forget about it and then download the corresponding version and create a project on gogs


PNG.

Something like this, and then find the Web hook in the repository Settings




PNG.

Fill in the address and domain name like this or whatever.

Next, write a server that responds to Webhook (why use Node because node is fast to configure the environment, of course you can also use Python java.NET, PHP).

//server.js // yarn add exec Var HTTP = require(' HTTP '), exec = require('exec') const PORT = 9988, PATH = '.. /html' var deployServer = http.createServer(function(request, response) { if (request.url.search(/deploy\/? $/i) > 0) { var commands = [ 'cd ' + PATH, 'git pull' ].join(' && ') exec(commands, function(err, out, code) { if (err instanceof Error) { response.writeHead(500) response.end('Server Internal Error.') throw err } process.stderr.write(err) process.stdout.write(out) response.writeHead(200) response.end('Deploy Done.') }) } else { response.writeHead(404) response.end('Not Found.') } }) deployServer.listen(PORT)Copy the code

Running in the background

Nodeserver.js & # If you want to stop #jobs -l #kill numberCopy the code

If you are using HTTP to deploy projects online, first set your Git user password. www.cnblogs.com/orzlin/p/56… Articles from this blog

With that said, the simplest automated deployment is done.

NPM install koA2-xxx package to install koA2-xxx package to install KOA2-xxx package.

Yarn init (or NPM init - use yarn just to type fewer words) yarn add koa --saveCopy the code

Now our project path looks like this

├ ─ ─ app. Js ├ ─ ─ package. The jsonCopy the code

Write the app. Js

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
    await next();
    ctx.response.type = 'text/html';
    ctx.response.body = '<h1>Hello Work</h1>'
});

app.listen(3000, () => {
    console.log('server is runding at http://loacalhost:3000');
});
Copy the code

Install Nodemon and use Nodemon app.js

yarn add nodemon --dev && cnpm i -g nodemon
Copy the code


PNG.

To see that means you’ve succeeded in the first step

The next step is to use koA-Router to handle routing problems

yarn add koa-router --dev
Copy the code

Create router.js in the root directory

const router = require('koa-router')();
const HomeController = require('./controller/home');

module.exports = (app) => {
    router.get('/',HomeController.index);

    router.get('/home',HomeController.home)

    app.use(router.routes())
    .use(router.allowedMethods())
}
Copy the code

./controller/home.js

module.exports = { index: async (ctx, next) => { ctx.response.body = 'index page'; }, home: async (ctx, next) => { console.log(ctx.request.query); console.log(ctx.request.querystring); ctx.response.body = '<h1>HOME page</h1>'; }};Copy the code

App. Js modified

const Koa = require('koa');
const app = new Koa();
const router = require('./router')

router(app)

app.listen(3000, () => {
    console.log('server is runding at http://loacalhost:3000');
});
Copy the code

Use koA-BodyParser for URL and POST parameters

yarn add koa-bodyparser --save
Copy the code

Continue to modify

const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const app = new Koa();
const router = require('./router');

app.use(bodyParser());

router(app);

app.listen(3000, () => {
    console.log('server is runding at http://loacalhost:3000');
});
Copy the code

You can also use koa-nunjucks-2.

yarn add koa-views ejs --save
Copy the code

Add. / views/index. Ejs

<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, <meta HTTP-equiv =" x-UA-compatible "content=" IE =edge"> <title>Document</title> </head> <body> <%= title%> </body> </html>Copy the code

Continue to modify app.js

const Koa = require('koa'); const bodyParser = require('koa-bodyparser'); const app = new Koa(); const router = require('./router'); const views = require('koa-views'); const path = require('path'); Use (views(path.join(__dirname, './views'), {extension: 'ejs'})) app.use(bodyParser()); router(app); app.listen(3000, () => { console.log('server is runding at http://loacalhost:3000'); });Copy the code

The home. Js

module.exports = { index: async (ctx, next) => { await ctx.render('index',{ title:'google' }); }, home: async (ctx, next) => { console.log(ctx.request.query); console.log(ctx.request.querystring); ctx.response.body = '<h1>HOME page</h1>'; }};Copy the code

Modify the following




PNG.

Handle static resources install KOA-static

yarn add koa-static --save
Copy the code

Modify app.js to add and specify /public as static resource directory.

const Koa = require('koa'); const bodyParser = require('koa-bodyparser'); const app = new Koa(); const router = require('./router'); const views = require('koa-views'); const path = require('path'); Koa-static const staticFiles = require('koa-static') Use (staticFiles(path.resolve(__dirname, ". / public "))) / / load the template engine app. Use (views (path. Join (__dirname, '. / views), {the extension: 'ejs'})) app. Use (bodyParser ()); router(app); app.listen(3000, () => { console.log('server is runding at http://loacalhost:3000'); });Copy the code

So you have a basic framework