@@ -4,7 +4,7 @@ const express = require('express');
44const bodyParser = require ( 'body-parser' )
55const app = express ( ) ;
66const mysql = require ( "mysql" ) ;
7- var connection ;
7+ let connection ;
88
99function createConnection ( ) {
1010 connection = mysql . createConnection ( {
@@ -19,28 +19,23 @@ function createConnection() {
1919app . use ( bodyParser . urlencoded ( {
2020 extended : false
2121} ) ) ;
22- //设置静态文件 app.js根目录下寻找public文件夹作为静态文件夹
22+ // Set static files directory
2323app . use ( express . static ( 'public' ) ) ;
2424// parse application/json
25- //是要get请求并且匹配到路由`/`,我就执行回调,并用`res.send`方法去相应结果
25+ // Handle GET request for root path
2626app . get ( '/' , function ( req , res ) {
2727 res . send ( 'Hello World' ) ;
2828} ) ;
2929
30- //中间件
30+ // Middleware: fetch paginated job listings
3131app . get ( '/index' , function ( req , res ) {
3232 createConnection ( )
3333 connection . connect ( ) ;
34- console . log ( req . query )
3534 const pageCount = ( req . query . page - 1 ) * 10 ;
36- //SELECT * FROM jobs WHERE position_id = 3067990 LIMIT 100,10
37- //SELECT * FROM jobs LIMIT 0,10
38- console . log ( 'SELECT * FROM jobs LIMIT ' + pageCount + ',10' )
3935 connection . query ( 'SELECT * FROM jobs LIMIT ' + pageCount + ',10' , function ( error , results , fields ) {
4036 if ( error ) throw error ;
41- //results =>array类型
42- console . log ( 'The solution is: ' , results ) ;
43- const obj = {
37+ //results => array type
38+ console . log ( 'The solution is: ' , results ) ; const obj = {
4439 jobs : results
4540 }
4641 res . send ( JSON . stringify ( obj ) ) ;
@@ -49,23 +44,22 @@ app.get('/index', function(req, res) {
4944 console . log ( req . query )
5045 res . append ( "Access-Control-Allow-Origin" , "*" )
5146 } )
52- //要post请求,并且路由是/home才能进入此逻辑
47+
48+ // Handle POST request for /home
5349app . post ( '/home' , function ( req , res ) {
5450 console . log ( req . body )
5551 res . append ( "Access-Control-Allow-Origin" , "*" )
56- res . send ( '进入到home页面 ' ) ;
52+ res . send ( 'Home page ' ) ;
5753} )
5854
59- //只要路由是/test就进入到此逻辑
55+ // Handle all methods for /test
6056app . all ( '/test' , function ( req , res ) {
6157 console . log ( req . cookies )
62- res . send ( '进入到test页面 ' ) ;
58+ res . send ( 'Test page ' ) ;
6359} )
6460
6561const server = app . listen ( 8081 , function ( ) {
66- //测试
67- //测试
6862 const host = server . address ( ) . address
6963 const port = server . address ( ) . port
70- console . log ( "应用实例,访问地址为 http://%s:%s" , host , port )
64+ console . log ( "App listening at http://%s:%s" , host , port )
7165} )
0 commit comments