我遇到了Node JS的麻煩。如何使用Node js Express創建獨立的可執行js文件,而無需定義路由?
藉助Apache/php模型,我可以獨立製作一個獨立的save.php文件(接受一個帖子請求,保存到一個txt文件中)而不會與Apache服務器混淆。
<?php file_put_contents ('content.txt' , $_POST['myData']);?>
在節點,我有server.js開始成爲了我在/公共任何文件:
var express = require('express');
var app = express();
app.use(express.static('public'));
app.use(express.static(__dirname + '/public'));
app.use(function(req,res, next){
if(req.accepts('html')){
res.status(404).sendFile(__dirname+'/public/404.html');
}
});
app.listen(process.env.PORT || 80);
我怎樣才能讓一個save.js文件,例如/public/test_project1/save.js,可以在HTML表單提交中執行?
var fs = require('fs');
fs.writeFile("content.txt", ???post data???)
有什麼辦法避免明確定義app.post()......在每一次server.js我做出/公衆一個新的js文件?我正在尋找一種架構,允許我創建一個節點服務器來承載/ public中的各個單獨項目文件
[Express Static nodejs]的可能重複(http://stackoverflow.com/questions/7069360/express-static-nodejs) – Jordonias