因此,几天来我一直在努力解决这个问题,几乎尝试了所有遇到的论坛建议。基本上,我已经设置了一个后端服务器,以便允许我自己通过节点发送电子邮件。奇怪的是,它在我的本地计算机上运行良好,甚至在我推送到Heroku时也可以完美构建和部署。但是,仍然会提示我“找不到路径/”。
这是我的文件结构:
PROJETNAME
- my-website
^--
^-- node_modules
^-- public
^-- src
- node_modules
- .gitignore
- package.lock.json
- package.json
- index.js
PACKAGE.JSON后端
{
"name": "myWebsite",
"version": "1.0.0",
"engines": {
"node": "10.9.0"
},
"description": "",
"main": "index.js",
"scripts": {
"client-install": "npm install --prefix my-website",
"start": "node index.js",
"server": "nodemon index.js",
"client": "npm run start --prefix my-website",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild":"NPM_CONFIG_PRODUCTION=false npm install --prefix my-website && npm run build --prefix my-website"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"concurrently": "^4.1.0",
"express": "^4.16.4",
"node-sass": "^4.11.0",
"nodemailer": "^6.0.0",
"nodemon": "^1.18.10"
}
}
这是我的index.js
const express = require('express')
const bodyParser = require('body-parser')
const nodemailer = require('nodemailer')
const app = express()
const path = require('path')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended:false}))
app.post("/api/form", (req, res) => {
const output = `
<ul>
<li>Bedrijfsnaam: ${req.body.companyName}</li>
<li>Naam: ${req.body.name} ${req.body.lastName}</li>
<li>Email: ${req.body.email}</li>
<li>Vraag: ${req.body.moreInfo}</li>
</ul>
`;
let transporter = nodemailer.createTransport({
host: "smtp-mail.outlook.com",
port: 587,
secure: false,
auth: {
user: '****',
pass: '***'
},
tls: {
ciphers: 'SSLv3',
}
});
let mailOptions = {
from: `***`,
to: "***",
subject: "***",
// text: "node test",
html: output // html body
};
let info = transporter.sendMail(mailOptions, (error)=> {
if (error) {
console.log(error)
}
})
});
if (process.env.NODE_ENV === 'production') {
// Exprees will serve up production assets
app.use(express.static('my-website/build'));
// Express serve up index.html file if it doesn't recognize route
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'my-website', 'build', 'index.html'));
});
}
//build mode
// app.get('*', (req, res) => {
// res.sendFile(path.join(__dirname + '/my-website/public/index.html'));
// })
const PORT = process.env.PORT || 3001
app.listen(PORT, () => {
console.log(`werkt op ${PORT}`)
})
和这个package.json在我的网站文件夹中
{
"name": "my-website",
"version": "0.1.0",
"engines": {
"node": "10.9.0"
},
"private": true,
"proxy": "http://localhost:3001",
"dependencies": {
"bootstrap": "^4.3.1",
"chokidar": "^2.1.5",
"node-sass": "^4.11.0",
"npm-run-all": "^4.1.5",
"react": "^16.8.6",
"react-anchor-link-smooth-scroll": "^1.0.12",
"react-dom": "^16.8.6",
"react-reveal": "^1.2.2",
"react-scripts": "2.1.8"
},
"scripts": {
"build-css": "node-sass --include-path ./src --include-path ./node_modules src/ -o src/",
"watch-css": "npm run build-css && node-sass --include-path ./src --include-path ./node_modules src/ -o src/ --watch --recursive",
"start": "npm-run-all -p watch-css start-js",
"build": "npm run build-css && react-scripts build",
"start-js": "react-scripts start"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
这是我的heroku日志
-----> Node.js app detected
-----> Creating runtime environment
NPM_CONFIG_LOGLEVEL=error
NODE_ENV=production
NODE_MODULES_CACHE=true
NODE_VERBOSE=false
-----> Installing binaries
engines.node (package.json): 10.9.0
engines.npm (package.json): unspecified (use default)
Resolving node version 10.9.0...
Downloading and installing node 10.9.0...
Using default npm version: 6.2.0
-----> Installing dependencies
Installing node modules (package.json)
> node-sass@4.11.0 install /tmp/build_6c50651e29b5891166dccce88ed95d14/node_modules/node-sass
> node scripts/install.js
Downloading binary from https://github.com/sass/node-sass/releases/download/v4.11.0/linux-x64-64_binding.node
Download complete
Binary saved to /tmp/build_6c50651e29b5891166dccce88ed95d14/node_modules/node-sass/vendor/linux-x64-64/binding.node
Caching binary to /tmp/npmcache.pCpCX/node-sass/4.11.0/linux-x64-64_binding.node
> node-sass@4.11.0 postinstall /tmp/build_6c50651e29b5891166dccce88ed95d14/node_modules/node-sass
> node scripts/build.js
Binary found at /tmp/build_6c50651e29b5891166dccce88ed95d14/node_modules/node-sass/vendor/linux-x64-64/binding.node
Testing binary
Binary is fine
> nodemon@1.18.11 postinstall /tmp/build_6c50651e29b5891166dccce88ed95d14/node_modules/nodemon
> node bin/postinstall || exit 0
Love nodemon? You can now support the project via the open collective:
> https://opencollective.com/nodemon/donate
added 468 packages from 264 contributors and audited 3018 packages in 14.79s
found 1 high severity vulnerability
run `npm audit fix` to fix them, or `npm audit` for details
-----> Build
Running heroku-postbuild
> myWebsite@1.0.0 heroku-postbuild /tmp/build_6c50651e29b5891166dccce88ed95d14
> NPM_CONFIG_PRODUCTION=false npm install --prefix my-website && npm run build --prefix my-website
up to date in 0.45s
found 0 vulnerabilities
npm ERR! path /tmp/build_6c50651e29b5891166dccce88ed95d14/my-website/package.json
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall open
npm ERR! enoent ENOENT: no such file or directory, open '/tmp/build_6c50651e29b5891166dccce88ed95d14/my-website/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! /tmp/npmcache.pCpCX/_logs/2019-04-19T07_08_11_217Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 254
npm ERR! myWebsite@1.0.0 heroku-postbuild: `NPM_CONFIG_PRODUCTION=false npm install --prefix my-website && npm run build --prefix my-website`
npm ERR! Exit status 254
npm ERR!
npm ERR! Failed at the myWebsite@1.0.0 heroku-postbuild script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /tmp/npmcache.pCpCX/_logs/2019-04-19T07_08_11_233Z-debug.log
-----> Build failed
We're sorry this build is failing! You can troubleshoot common issues here:
https://devcenter.heroku.com/articles/troubleshooting-node-deploys
If you're stuck, please submit a ticket so we can help:
https://help.heroku.com/
Love,
Heroku
! Push rejected, failed to compile Node.js app.
! Push failed