角度の2からノード/エクスプレスサーバーに連絡フォームデータを送信しようとしている初心者...今は組み込みサーバーで角2をホストしていますlocalhost:4200で、エクスプレスサーバーはlocalhost:3000です。私は、次のコードを試してみましたが、次のエラーを取得しています:ここで角度2からサーバーを表現する方法
Error: ENOENT: no such file or directory, stat 'C:\Users\corey\Desktop\Project\server\api\contact-form-submit'
は私の連絡先-form.component.tsファイルです。ここで
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-contact-form',
templateUrl: './contact-form.component.html',
styleUrls: ['./contact-form.component.css']
})
export class ContactFormComponent {
constructor(private http: HttpClient) {}
onSubmit(form) {
this.http.post('http://localhost:3000/server/api/contact-form-submit', JSON.stringify(form.value)).subscribe();
console.log(form.value);
}
}
は私server.jsファイルです:
var express = require('express');
var nodemailer = require('nodemailer');
var mg = require('nodemailer-mailgun-transport');
var bodyParser = require('body-parser');
var app = express();
var path = require('path');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
//trying to get contact form submission from angular 2 form here
app.get('/', function (req, res) {
res.sendfile('./api/contact-form-submit');
console.log('test');
});
//api key and domain for mailgun
var auth = {
auth: {
api_key: 'api-key-here',
domain: '[email protected] here'
}
}
//trying to send email with nodemailer here
app.post('./api/send', function(req, res) {
var transporter = nodemailer.createTransport(mg(auth));
var mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Website submission',
text: 'Name: ' + req.body.name + 'Email: ' + req.body.email + 'Message: ' + req.body.message,
html: '<p>Submission: </p><br><ul><li>Name: ' + req.body.name + '</li><li>Email: ' + req.body.email + '</li><li>Message: ' + req.body.message + '</li></ul>'
};
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log(error);
res.redirect('/');
} else {
console.log('Message sent.');
res.redirect('/');
}
})
});
app.listen(3000, function() {
console.log('Express started on port 3000');
});
Angular 2内で使用するフォームデータが正常に取得されました。エクスプレスサーバーには、htmlファイルを使用して定期的な速報をメールで送信していましたが、Angular wの統合方法の重要な部分を理解していませんi Expressを使用して、Nodemailerで電子メールを送信します。どんな助け?ありがとう。 This is my project structure.
ため、このgithubのレポhttps://github.com/kuncevic/angular-httpclient-examplesは 'app.post( './ API /送信'、' 'app.post( '/ API /送信' であるとから、あなたのAPIのルートURLをchaningしてみてください、 '(ドットなし) – Sgnl
また、サーバは' res.sendfile( './ api/contact-form-submit'); 'この行からあなたが望むファイルをファイルに書き込むことができないため、エラーが発生する可能性があります。ファイルシステムとおそらくファイルが存在しないので、ファイルを見つけることができないというエラーが発生します。 – Sgnl