Я пытаюсь создать базовое веб-приложение, используя API карт Google с экспресс / узлом. В настоящее время мои три файла следующие:
server.js
const express = require('express');
const bodyParser = require('body-parser');
const app = express()
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs')
// Initialize and add the map
function initMap() {
// The location of Uluru
var uluru = {
lat: -25.344,
lng: 131.036
};
// The map, centered at Uluru
var map = new google.maps.Map(
document.getElementById('map'), {
zoom: 4,
center: uluru
});
// The marker, positioned at Uluru
var marker = new google.maps.Marker({
position: uluru,
map: map
});
}
app.get('/', function (req, res) {
res.render('index');
})
app.post('/', function (req, res) {
res.render('index');
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
style.css
#map {
height: 400px;
width: 100%;
}
index.html
<h3>My Google Maps Demo</h3>
<div id="map"></div>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=API&callback=initMap">
</script>
Когда я запускаю приложение, возвращается ошибка «initMap - это не функция». Я читал примеры в Интернете, но все, что я пробовал, не сработало. Я хотел бы сохранить как можно меньше JavaScript-файла от моего HTML-файла.