Node.js,我如何获取HTML表单文本输入并将其指向本地文件

时间:2017-08-16 13:20:08

标签: html node.js ejs

我试图通过表单输入一些非常简单的文本并将其指向.txt文件。没有太多的运气,我的方式超出了我的深度,任何帮助赞赏。

我尝试了几种不同的方法但是atm我正在尝试获取输入,将其转换为json数据并直接发送到文件。

app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var http = require('http');
var fs = require('fs');
var passport = require('passport');
var passportLocal = require('passport-local');
var expressSession = require('express-session');
var expressValidator = require('express-validator')


//routes setup
var index = require('./routes/index');
var homepage = require('./routes/homepage');
var login = require('./routes/login');
 var charitySignUp = require('./routes/charitySignUp');
var about = require('./routes/about');
var contactUs = require('./routes/contactUs');
var charityHomepage = require('./routes/charityHomepage');
var chat = require('./routes/newsFeed');
var ticket = require ('./routes/ticket');


var app = express();


require('./public/configure/passport');


// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');


//middleware
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
//app.use(session({secret: 'mySecret', resave: false, saveUninitialized: 
false}));
//app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));

//routes + backend js file
app.use('/', index);
app.use('/login', login);
app.use('/homepage', homepage);
app.use('/charitySignUp', charitySignUp);
app.use('/about', about);
app.use('/contactUs', contactUs);
app.use('/charityHomepage', charityHomepage);
app.use('/chat', chat);
app.use('/ticket', ticket);


  / catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');


});


module.exports = app;

Ticket.js - 后端代码

var express = require('express');
var router = express.Router();
var fs = require('fs');



router.get('/', function (req, res, next) {
res.render('ticket');
});


router.get('/insert', function (req, res) {


var issue = req.query.issue;
var email = req.query.email;
fs.appendFile("my_file.txt", JSON.stringify(email + issue), function (err) {
    if (err) throw err;
    console.log('updated');
});
});


module.exports = router;

.EJS文件中的表单。

 <form class="form-signin" action="/insert" method="get">
    <h2 class ="text" class="form-signin-heading">Ticket form</h2>



    <label class="sr-only"><b>email</b></label>
    <input type="Email" class="form-control" placeholder="Enter email you would like your response directed" name="email" required>

    <label class="sr-only"><b>Issue</b></label>
    <input type="text" class="form-control" placeholder="type here" name="issue" required>

    <button type="submit" class="btn btn-lg btn-primary btn-block">Submit</button>



   </form>

0 个答案:

没有答案