一个调查网站,Node.js数据收集anamoly上heroku

我正在一个包含表单的网站上工作。 我的要求是收集每个用户的所有表单数据。

它是一个资金紧张的学术项目,因此我使用了heroku免费服务器空间。

我select了node.jsexpression式框架。 创build一个简单的表单,并在提交时,它写入一个json的文件。 以下是代码。

//require the express nodejs module var express = require('express'), //set an instance of exress app = express(), //require the body-parser nodejs module bodyParser = require('body-parser'), //require the path nodejs module path = require("path"); var fs = require("fs"); //support parsing of application/json type post data app.use(bodyParser.json()); //support parsing of application/x-www-form-urlencoded post data app.use(bodyParser.urlencoded({ extended: true })); //tell express that www is the root of our public web folder app.use(express.static(path.join(__dirname, 'www'))); //tell express what to do when the /form route is requested app.post('/form',function(req, res){ res.setHeader('Content-Type', 'application/json'); //mimic a slow network connection setTimeout(function(){ res.send(JSON.stringify({ firstName: req.body.firstName || null, lastName: req.body.lastName || null })); var fileContent = JSON.stringify(req.body); fs.writeFile("./sample.txt", fileContent, (err) => { if (err) { console.error(err); return; }; console.log("File has been created"); }); }, 1000) //debugging output for the terminal console.log('you posted: First Name: ' + req.body.firstName + ', Last Name: ' + req.body.lastName); }); app.set('port', (process.env.PORT || 5000)); app.listen(app.get('port'), function() { console.log("Node app is running at localhost:" + app.get('port')); }); 

当我在localhost上运行它时,它完美地工作,创build一个文件。

但是当我在heroku上部署它,并运行,然后克隆回购,没有文件。

虽然这是低级别的问题,但在更高的层面上,我只想要一个具有dynamic表单和服务器空间的网页来收集这些数据。 我不想在这个活动上花很多时间,因为我真正的工作是基于数据。 我很确定github页面不起作用。 我们整合了谷歌表格,它可以工作,但网页不是dynamic的。 因此,从高层次问题到低层次问题,我的具体问题是:

  1. 有创build一个网页和收集数据的框架吗? heroku是一个灵活的select吗? 数据生成不会很大,我们期望每周的数据量<20MB。

  2. 是否必须有像MySQL或mongoDB数据库连接,然后只有我才能够收集数据? 如果是的话,我可以有一个快速教程来展示这个任务的“hello world”吗?

  3. 在这个特定的问题中,什么是错误的,因为它在本地主机上以我想要的方式工作。

我非常乐意接受新的build议,这可能与我目前的思维过程完全不同,但为我节省了一段时间。 感谢您的帮助..

Interesting Posts