如何避免将localhost添加到.js src
我完全陷入了困境。一旦我用node app.js
和node watch
发布我的应用程序后,我进入控制台跟随:Загрузка <script> по адресу «http://localhost:8000/sheetrock.js» не удалась.
什么意思是从localhost加载脚本无法管理。但是在我的.html中我说
<script type="text/javascript" src="./sheetrock.js"></script>
所以src中没有localhost。我想要的是src属性不是相对的,而是静态的(没有添加localhost)。我的代码有什么问题? App.js如下:
const path = require('path');
const express = require('express');
const ejs = require('ejs');
const paypal = require('paypal-rest-sdk');
const exphbs = require('express-handlebars');
const app = express();
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
// app.set('views', path.join(__dirname, 'views'));
app.get('/', (req, res) => res.render('index'));
app.get('/success', (req, res) => {
const payerId = req.query.PayerID;
const paymentId = req.query.paymentId;
const execute_payment_json = {
"payer_id": payerId,
"transactions": [{
"amount": {
"currency": "USD",
"total": "25.00"
}
}]
};
paypal.payment.execute(paymentId, execute_payment_json, function (error, payment) {
if (error) {
console.log(error.response);
throw error;
} else {
console.log(JSON.stringify(payment));
res.send('Success');
}
});
});
app.get('/cancel', (req, res) => res.send('Cancelled'));
app.listen(8000, () => console.log('Server Started'));
回答如下:
你应该尝试像这样服务index.html
app.get('/', (req,res) => {
res.sendFile(path.join(__dirname + '/index.html'))
})