zoukankan      html  css  js  c++  java
  • node.js

    一.1.1 Express4.16.3 基于 Node.js 平台,快速、开放、极简的 Web 开发框架

    Express 提供精简的基本 Web 应用程序功能,而不会隐藏您了解和青睐的 Node.js 功能。

    const http = require('http')
    const fs = require('fs')
    
    const app = http.createServer((req, res) => {
      switch (req.url) {
        case '/':
          res.write('home')
          res.end('')
        break
        case '/index.html':
          fs.readFile('./pages/index.html', 'utf8', (err, data) => {
            res.write(data)
            res.end('')
          })
        break
        case '/ .js':
          fs.readFile('./app.js', 'utf8', (err, data) => {
            res.write(data)
            res.end('')
          })
        break
        default:
          res.write('404') 
          res.end('')
      }
    })   
    
    app.listen(3000, () => {
      console.log('localhost:3000')
    })
    
    

    1.2使用express框架时访问资源

    app.use(express.static(path.join(__dirname, 'public')));
    

    二。RMVP设计模式的实现

        //app.js
        // 开发接口
            app.use('/api/position', positionRouter)
    

    R.>>>>>

        //    R.>>>>>
        const express = require('express')
        const positionController = require('../controllers/position')  //调用controller层
    
        const router = express.Router()
    
        // 注意,listall不要调用
        router.get('/listall', positionController.listall)  //和/api/position拼接 post也是一样
        router.post('/save', () =>{
            res.header('Content-Type', 'application/json; charset=utf-8')//res的表头定义 返回给前端的类型 form是post query是get  post json是request payload
        })
    
        //后端是不能解析json字符串的 使用bodyparse
    
        module.exports = router
    
  • 相关阅读:
    谈一谈循环的性能提升
    Web前端性能优化的9大问题
    随机获取一种颜色值的三种方法
    ES6还是ES2015?
    history.back(-1)和history.go(-1)的区别
    关于overflow-y:scroll ios设备不流畅的问题
    前端如何压缩图片
    【转】理解JavaScript之闭包
    关于如何给数字排序
    本地缓存localstorage使用
  • 原文地址:https://www.cnblogs.com/moonzwt/p/9831589.html
Copyright © 2011-2022 走看看