基于Express.js框架,使用RESTful Api

关于项目中的 service.js
我将'gnomeontherun'演示项目中所使用的配置代码黏贴上来.
来自: https://github.com/ionic-in-action/chapter3


//注意,如果你没有配置依赖相关框架将会报错
//你可以手动单独安装这些框架

//npm install body-parser
//npm install express --save

//express是什么? Look This →_→ [url]http://www.expressjs.com.cn/[/url]
var express = require('express');

//这是nodeJs的文件系统模块,是一个简单包装的标准 POSIX 文件 I/O 操作方法集.
//这里之所以要使用我看他想读取一些演示数据 'data.json'
var fs = require('fs');

//bodyParser用于解析客户端请求的body中的内容,内部使用JSON编码处理,url编码处理以及对于文件的上传处理.
var bodyParser = require('body-parser');

var app = express();

app.use(express.static(__dirname));
app.use(bodyParser.json());
app.use(function (req, res, next) {
  res.set('Content-Type', 'application/json');
  next();
});

//这是一个app的http请求
app.get('/notes', function (req, res) {
  // Open the existing notes file
  fs.readFile(__dirname + '/data/notes.json', 'utf8', function (err, data) {

    // If we get an error, log it and return
    if (err) {
      res.status(500).end();
      return console.log(err);
    }

    res.status(200).send(data);

  });
})

// Update a note
app.put('/notes/:id', function (req, res) {
  // Open the existing notes file
  fs.readFile(__dirname + '/data/notes.json', 'utf8', function (err, data) {

    // If we get an error, log it and return
    if (err) {
      res.status(500).end();
      return console.log(err);
    }

    // Try to parse the JSON or return
    try {
      data = JSON.parse(data);
    } catch (e) {
      res.status(500).end();
      return console.log(e);
    }

    // Add body item to notes array
    data.forEach(function (note, index) {
      if (note.id == req.params.id) {
        data[index] = req.body;
      }
    });

    // Write file back to server
    fs.writeFile(__dirname + '/data/notes.json', JSON.stringify(data), function (err) {

      // If we get an error, log it and return
      if (err) {
        res.status(500).end();
        return console.log(err);
      }

      // No errors, everything is done so return new data
      res.status(200).send(data);
    });
  });
});

// Create a new note
app.post('/notes', function (req, res) {
  // Open the existing notes file
  fs.readFile(__dirname + '/data/notes.json', 'utf8', function (err, data) {

    // If we get an error, log it and return
    if (err) {
      res.status(500).end();
      return console.log(err);
    }

    // Try to parse the JSON or return
    try {
      data = JSON.parse(data);
    } catch (e) {
      res.status(500).end();
      return console.log(e);
    }

    // Add body item to notes array
    data.push(req.body);

    // Write file back to server
    fs.writeFile(__dirname + '/data/notes.json', JSON.stringify(data), function (err) {

      // If we get an error, log it and return
      if (err) {
        res.status(500).end();
        return console.log(err);
      }

      // No errors, everything is done so return new data
      res.status(201).send(data);
    });
  });
});

// Delete a note
app.delete('/notes/:id', function (req, res) {
  // Open the existing notes file
  fs.readFile(__dirname + '/data/notes.json', 'utf8', function (err, data) {

    // If we get an error, log it and return
    if (err) {
      res.status(500).end();
      return console.log(err);
    }

    // Try to parse the JSON or return
    try {
      data = JSON.parse(data);
    } catch (e) {
      res.status(500).end();
      return console.log(e);
    }

    // Add body item to notes array
    var index = -1;
    data.forEach(function (note, i) {
      if (note.id == req.params.id) {
        index = i;
      }
    });

    // If we found an item by that id, remove it
    if (index >= 0) {
      data.splice(index, 1);
    }

    // Write file back to server
    fs.writeFile(__dirname + '/data/notes.json', JSON.stringify(data), function (err) {

      // If we get an error, log it and return
      if (err) {
        res.status(500).end();
        return console.log(err);
      }

      // No errors, everything is done so return
      res.status(204).end();
    });
  });
});

//App的web端口监听在这里配置.
app.listen(3000, function () {
  console.log('Server started. Open http://localhost:3000 in your browser.');
});

猜你喜欢

转载自784838898.iteye.com/blog/2354923