fcs
2025-04-10 0163870d662dc5b7d69ceff977490ede594f7d82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * User: Kurten
 * Date: 14-3-1
 * Time: 14:38
 * Version: 1.0
 * Description:
 */
var http = require('http');
var url = require('url');
var snowflake = require('./snowflake');
 
module.exports = function(port) {
    http.createServer(function (req, res) {
        var uobj = url.parse(req.url, true);
        if (uobj.pathname == '/next_id') {
            res.writeHead(200, {'Content-Type': 'application/json'});
            var query = uobj.query || {};
            var id = snowflake.nextId(query.worker_id, query.data_center_id, query.sequence);
            res.end(JSON.stringify({id : id}));
        } else {
            res.writeHead(404, {'Content-Type': 'application/json'});
            res.end(JSON.stringify({status : 404, msg : 'request url error, not found url'}));
        }
    }).listen(port || 3000);
 
    console.log('now server listen port ' + (port || 3000));
 
    process.on("uncaughtException", function (err) {
        console.trace("uncaughtException:" + err.stack);
    });
};