javascript - 如何避免将 localhost 添加到 .js src
问题描述
我完全陷入了追随。一旦我启动我的应用程序node app.js
并在node watch
我进入控制台之后:Загрузка <script> по адресу «http://localhost:8000/sheetrock.js» не удалась.
什么意味着从本地主机加载脚本没有管理。但是在我的 .html 中我说
<script type="text/javascript" src="./sheetrock.js"></script>
所以src中没有localhost。我想要的是 src 属性不是相对的,而是静态的(没有添加本地主机)。我的代码有什么问题?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'))
})
推荐阅读
- javascript - 按星期和日期对 DataTable 进行排序
- node.js - 如何在节点js中显示来自api(计算机视觉-azure中的缩略图服务)响应的网页中的图像
- symfony - 如何从上面的当天停用日期
- python - 如何使用两个列表制作字典,但我想要两个作为值并且键将是名称和 sys-id?
- webview - 有什么方法可以使用 Url_launcher 插件在 Iphone 浏览器上隐藏 url
- javascript - 在重新渲染之间,对 redux 存储的对象引用是否保持不变
- ruby - Nokogiri SAX 忽略空字符
- python - 如何使用 Matplotlib 使 x 轴更清晰?
- wordpress - Meta_key 和 meta_value - Wordpress
- java - 流式写入 csv 文件