Using POST data to write to local file with node.js and express

前端 未结 3 1624
被撕碎了的回忆
被撕碎了的回忆 2020-12-01 15:37

I\'m trying to just handle simple POST requests and append the data to a local file. However, when I try to POST raw text with postman, such as \'hi world\', what\'s actuall

3条回答
  •  抹茶落季
    2020-12-01 15:53

    var express = require('express'),
        fs = require('fs'),
        url = require('url');
    var app = express();
    
    app.use('/public', express.static(__dirname + '/public'));  
    app.use(express.static(__dirname + '/public')); 
    
    app.post('/receive', function(request, respond) {
        var body = '';
        filePath = __dirname + '/public/data.txt';
        request.on('data', function(data) {
            body += data;
        });
    
        request.on('end', function (){
            fs.appendFile(filePath, body, function() {
                respond.end();
            });
        });
    });
    
    app.listen(8080);
    

提交回复
热议问题