美文网首页
node-express

node-express

作者: O蚂蚁O | 来源:发表于2022-03-31 13:52 被阅读0次

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

    强大的express express三步走: 创建服务器 var app =express(); 给服务器添加监听...

  • node-express

    原因: 感觉对后端知识很模糊 工具 node express框架express generator 官方推荐全局安...

  • node-express

    1、安装node node官网:https://nodejs.org/zh-cn/[https://nodejs....

  • 项目地址

    1. 以express为基础的node.js项目 node-express: 一个基于express写的node项...

  • node-express模块

    什么是express: 地址:https://www.expressjs.com.cn/ 安装:npm i exp...

  • node-Express 中间件

    Middleware,以后提到这个词的时候,真不清楚到底说的是那般? 最近在看松哥代码,然后查了这个博客, htt...

  • node-express | 实现登录验证和授权

    账号登录验证和权限设置,可以说是任何一个系统都必须具备的基本功能,而方式是从最传统的账号密码登录,到社交账号绑定不...

  • node-express不能操作mysql解决办法

    express连接操作数据库 use mysql;update user set authentication_s...

  • node-express连接mysql实现增删改查

    继上一篇的代码在index.js的同级目录下新建一个router.js和mysql.js文件 连接数据库 终端执行...

  • 基于Node-Express和MongoDB的微型博客系统

    源码及相关配置信息在文末的GitHub地址里,欢迎下载和分享。 其主要有以下几个功能: 用户注册 用户登录 用户编...

网友评论

      本文标题:node-express

      本文链接:https://www.haomeiwen.com/subject/jospqrtx.html