node-express

1、安装node

node官网:https://nodejs.org/zh-cn/

2、express安装

npm install express --save

3、新建项目

express myExpress

4、项目目录

image.png

5、文件

package.json

{
  "name": "pdm-node",
  "version": "1.0.0",
  "main": "index.js",
  "bin": {
    "pdm-node": "./bin/www.js"
  },
  "scripts": {
    "start": "nodemon ./bin/www.js",
    "build": "webpack --config webpack.config.js"
  },
  "repository": {
    "type": "git",
    "url": "git地址"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "body-parser": "^1.19.1",
    "cors": "^2.8.5",
    "express": "^4.17.2",
    "fs": "^0.0.1-security",
    "morgan": "^1.10.0",
    "mysql": "^2.18.1",
    "nodemon": "^2.0.15",
    "request": "^2.88.2",
    "url": "^0.11.0",
    "webpack": "^5.65.0",
    "webpack-cli": "^4.9.1"
  },
  "dependencies": {
    "moment": "^2.29.1"
  }
}

app.js

const express = require("express")
const cors = require('cors')
const path = require('path')
const app = express()
var bodyParser = require('body-parser')
var logger = require('morgan');
const indexRouter = require('./router/index')
const bodyParse = require('./custom-middleware/query-body-parse')
const returnData = require('./custom-middleware/return-data')
app.use(cors())
app.use(logger('dev'));
app.use(express.static(path.join(__dirname,'static')));
app.use(express.urlencoded({extended: false}))
app.use(express.json());
// app.use(bodyParse)
app.use(bodyParser.json())
app.use(returnData)

app.use('/', indexRouter)

module.exports = app

www.js

#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
var debug = require('debug')('demol1: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);
}

router index.js --- 路由出口文件

const express = require('express');
const router = express.Router()
const gitIndex = require('./git/index')
router.use(gitIndex)
module.exports = router

#### router list.js

const express = require('express');
const router = express.Router()
const db = require('../../db/index');
// 获取列表
router.get('/list', function(req,res) {
console.log('process', process)
db.query('select * from 表名', function(err,data) {
if(err) {
res.send({ ...res.postData, data: err.message})
return
}
res.send({ ...res.postData, data: data})
})
})
module.exports = router

你可能感兴趣的:(node-express)